c# windows服务状态、启动和停止服务
ServiceController sc = new ServiceController("Server");建立服务对象
//服务运行则停止服务
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped); //等待停止
sc.Refresh();
}
//服务停止则启动服务
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running); //等待启动
sc.Refresh();
}
浙公网安备 33010602011771号