开发者

How to start /stop a windows service through a Windows Form application

I wish to start and stop another Windows Service application via my WIndows Form application. Ons tart of 开发者_运维百科my application I wish to start the Windows service, and on application exit I wish to stop the service.

How can this be achieved?


You want to use the ServiceController class.

Initialize it with your service name, and call Start() and Stop() methods.

using System.ServiceProcess;
ServiceController sc = new ServiceController("My service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
  sc.Start();
}
//etc..
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜