开发者

Run windows service from winform

开发者 https://www.devze.com 2022-12-14 11:11 出处:网络
How can i control (st开发者_开发百科art, stop) windows service from my windows application?// ADD \"using System.ServiceProcess;\" after you add the

How can i control (st开发者_开发百科art, stop) windows service from my windows application?


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


What do you mean by "run"? If you want to control (start, stop and otherwise manipulate) services installed on your local (or remote) machine, ServiceController is the way to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消