/// <summary>
 /// 停止Windows服务
 /// </summary>
 /// <param name="serviceName">服务名</param>
 /// <returns>true: 已停止; false: 状态未知</returns>
 public bool StopService(string serviceName)
 {
     if (ServiceExisted(serviceName))
     {
         ServiceEntity serviceEntity = Services[serviceName];
         if (serviceEntity.Status == ServiceControllerStatus.Running)
         {
             serviceEntity.Service.Stop();
             for (int i = 0; i < 60; i++)
             {
                 Thread.Sleep(1000);
                 serviceEntity.Service.Refresh();
                 serviceEntity.Status = serviceEntity.Service.Status;
                 if (serviceEntity.Status == ServiceControllerStatus.Stopped)
                 {
                     return(true);
                 }
             }
             return(false);
         }
         else
         {
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
 public void Add(ServiceEntity entity)
 {
     if (services.ContainsKey(entity.ServiceName))
     {
         return;
     }
     else
     {
         services.Add(entity.ServiceName, entity);
     }
 }
        /// <summary>
        /// Windows服务是否已经启动
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <returns>true: 已启动; false: 未启动(或服务不存在)</returns>
        public bool ServiceStarted(string serviceName)
        {
            ServiceEntity serviceEntity = Services[serviceName];

            if (serviceEntity != null)
            {
                serviceEntity.Service.Refresh();
                serviceEntity.Status = serviceEntity.Service.Status;
                return(serviceEntity.Status == ServiceControllerStatus.Stopped);
            }
            else
            {
                return(false);
            }
        }
 public void Remove(ServiceEntity entity)
 {
     Remove(entity.ServiceName);
 }
 public static int CompareByServiceName(ServiceEntity a, ServiceEntity b)
 {
     return(string.Compare(a.ServiceName, b.ServiceName));
 }