从winform运行Windows服务

时间:2009-12-22 06:36:38

标签: windows service

如何从Windows应用程序控制(启动,停止)Windows服务?

2 个答案:

答案 0 :(得分:3)

// ADD "using System.ServiceProcess;" after you add the 
// Reference to the System.ServiceProcess in the solution Explorer
using System.ServiceProcess;

ServiceController myService = new ServiceController();    
myService.ServiceName = "ImapiService";

string svcStatus = myService.Status.ToString();

if (svcStatus == "Running")
{
    myService.Stop();
}
else if(svcStatus == "Stopped")
{
    myService.Start();
}
else
{
    myService.Stop();
}

答案 1 :(得分:1)

“跑”是什么意思?如果您想控制(启动,停止和以其他方式操纵)本地(或远程)计算机上安装的服务,ServiceController是可行的方法。