问题
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?
回答1:
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..
来源:https://stackoverflow.com/questions/24861635/install-start-and-stop-windows-service-form-winforms-application