从winform运行windows服务

问题描述:

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

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

“run”是什么意思?如果您想要控制(启动,停止和以其他方式操作)安装在本地(或远程)机器上的服务,则需要使用ServiceController