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();
             }

posted @ 2011-12-22 14:38  therockthe  阅读(1910)  评论(0)    收藏  举报