天天看點

c# windows服務狀态、啟動和停止服務

<col>

首先先引用System.ServiceProcess.dll

然後在引用命名​​​​​​空間using System.ServiceProcess;

ServiceController sc = new ServiceController("Server");建立服務對象

//服務運作則停止服務

             if (sc.Status.Equals(ServiceControllerStatus.Running))

             {

                 sc.Stop();

                 sc.Refresh();

             }

//服務停止則啟動服務

             if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending)))

                 sc.Start();