我已经为Windows服务创建了安装程序,并且从AfterInstall
事件我开始服务但是在启动服务之前我想检查服务是否没有运行然后我将启动服务否则不会但我不知道如何从AfterInstall
事件检查我的服务是否正在运行。请指导。感谢
using System.ServiceProcess;
class ServInstaller : ServiceInstaller
{
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
}
答案 0 :(得分:0)
您可以按如下所示进行检查: -
using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
// Your code when service stopped
}
}