C#Windows服务:一些方法(启动、停止等)
前面讲述了如何创建和安装服务(创建windows服务),下面把启动、停止、判断是否启动的方法也写一下。
/// <summary> /// 判断某个服务是否启动 /// </summary> /// <param name="serviceName"></param> public static bool ISStart(string serviceName) { bool result = true; try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { if ((service.Status == ServiceControllerStatus.Stopped) || (service.Status == ServiceControllerStatus.StopPending)) { result = false; } } } } catch { } return result; }