在网上搜了许久,但都跟话题相差太远,哪位高手能 给小弟提供一些资料或见解,小弟不胜感激...我在winform界面上放了两个按钮,一个是“注册Windows服务”,另一个“卸载服务”,现有一个要注册服务用的文件“DDS.exe”,放在\bin\Debug\下, 第一次运行“注册服务”可以,再点击任何一个按钮就都会i报错“配置系统未能初始化”,请较 大侠 这是为何?相关代码如下:
///
/// 注册服务方法
///
///
private void RegWinService(string strFileName)
{
string[] commandLine = { };
string serviceFileName = System.Environment.CurrentDirectory +"\\"+ DDS.exe;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Install(new System.Collections.Hashtable());
}
///
/// 卸载服务方法
///
///
private void UninstallWinService(string strFileName)
{
string[] commandLine = { };
string serviceFileName = System.Environment.CurrentDirectory +"\\"+ DDS.exe;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Uninstall(null);
}
///
/// 注册服务方法
///
///
private void RegWinService(string strFileName)
{
string[] commandLine = { };
string serviceFileName = System.Environment.CurrentDirectory +"\\"+ DDS.exe;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Install(new System.Collections.Hashtable());
}
///
/// 卸载服务方法
///
///
private void UninstallWinService(string strFileName)
{
string[] commandLine = { };
string serviceFileName = System.Environment.CurrentDirectory +"\\"+ DDS.exe;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Uninstall(null);
}
写两个批处理文件:
注册启动服务SetupService.bat:InstallUtil ******.exe
net start 服务名注销服务UnInstallService.bat
InstallUtil /u ******.exe然后在C#代码中执行这两个批处理文件。
IDictionary saveState = new Hashtable();
Installer.UseNewContext = true;
Installer.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DDS.exe");
saveState.Clear();
Installer.Install(saveState);
Installer.Commit(saveState);
Installer.Dispose();
卸载
AssemblyInstaller Installer = new AssemblyInstaller();
Installer.UseNewContext = true;
Installer.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DDS.exe");
Installer.Uninstall(null);
Installer.Dispose();