1.引用System.ServiceProcess命名空间

using System.ServiceProcess;

2.声明ServiceController变量

private ServiceController _controller;

 

3.假设服务名为ServicesName, 编写开始服务,停止服务,重启服务的代码如下

private void StopService()
{
    this._controller = new ServiceController("ServicesName");
    this._controller.Stop();
    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);
    this._controller.Close();
}

private void StartService()
{
    this._controller = new ServiceController("ServicesName");
    this._controller.Start();
    this._controller.WaitForStatus(ServiceControllerStatus.Running);
    this._controller.Close();
}


private void ResetService()
{
    this._controller = new ServiceController("ServicesName");
    this._controller.Stop();
    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);
    this._controller.Start();
    this._controller.WaitForStatus(ServiceControllerStatus.Running);
    this._controller.Close();
}

相关文章:

  • 2022-12-23
  • 2021-08-17
  • 2021-07-11
  • 2022-12-23
  • 2021-11-15
  • 2021-06-14
  • 2021-07-13
猜你喜欢
  • 2021-11-04
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2021-05-15
相关资源
相似解决方案