2
如何從Windows應用程序控制(啓動,停止)windows服務?從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。