Joh*_*ohn 5 .net c# windows-installer windows-services
作为安装我的Windows服务的一部分,我编写了installutil和命令行的替代方法:
IDictionary state = new Hashtable();
using (AssemblyInstaller inst = new AssemblyInstaller(typeof(MyServiceClass).Assembly, args))
{
IDictionary state = new Hashtable();
inst.UseNewContext = true;
//# Service Account Information
inst.Install(state);
inst.Commit(state);
}
Run Code Online (Sandbox Code Playgroud)
安装它.我通过检测它是否成功启动来确定是否需要这样做.我预计在请求启动和实际设置它的RunningOk标志之间会出现延迟,而宁愿采用通用的程序化解决方案.(如何在C#中以编程方式安装Windows服务?)http://dl.dropbox.com/u/152585/ServiceInstaller.cs解决方案已有将近3年的历史,并且从我所知道的很少进口DLL. NET似乎打败了它的安全意图.
因此,我想知道用.NET做一个更简洁的方法,如果存在的话?
Joh*_*ohn 11
要查找服务,以及它是否正在运行,
private static bool IsServiceInstalled(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
if (service.ServiceName == serviceName) return true;
return false;
}
private static bool IsServiceInstalledAndRunning(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
if (service.ServiceName == serviceName) return service.Status != ServiceControllerStatus.Stopped;
return false;
}
Run Code Online (Sandbox Code Playgroud)
请注意,它实际上会检查它是否未停止而不是它是否正在运行,但如果您愿意,可以更改它.
很高兴在简洁的.NET结构中这样做.不要记得我从哪里获得信息,但现在看起来很容易,值得发帖.