c# 启动和停止windows服务
发布时间:2015-10-17 编辑:jiaochengji.com
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();
}
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# 启动和停止windows服务
(图)cwRsync server服务启动失败的解决方法
命令行停止、启用、禁用某服务
禁用windows中的print spooler打印服务的简单方法
学习使用命令安装和卸载windows服务
解决office2013安装错误1921/1920的问题
win2003服务器安全设置之系统服务篇
修改windows2003的默认上传限制
用.NET创建Windows服务
编写 Window 服务程序
[关闭]