软件编程
位置:首页>> 软件编程>> C#编程>> 基于C#实现Windows服务状态启动和停止服务的方法

基于C#实现Windows服务状态启动和停止服务的方法

作者:shichen2014  发布时间:2022-07-27 12:17:57 

标签:C#,Windows,服务,启动,停止

本文以实例形式展示了基于C#实现Windows服务状态启动和停止服务的方法。非常实用。分享给大家供大家参考之用。具体方法如下:

首先先引用:


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

希望本文所述对大家的C#程序设计有所帮助

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com