常用工具小方法

  static void GetServiceState()
        {
          
            /* 描述: 启动服务
             * 添加引用:
               using System.ServiceProcess;
               using System.Management;
             */
            const string ServiceName = "SQLSERVERAGENT";
            Console.WriteLine(string.Format("启动服务:{0}", ServiceName));
            //ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", ServiceName));
            //ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
            //changeMethod["DesktopInteract"] = true;
            //ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);


            /*  启动服务代码 */
            ServiceController sc = new ServiceController(ServiceName);

            if (sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }
原文地址:https://www.cnblogs.com/rhythmK/p/3110665.html