How can I start and stop a windows service from a c# Form application?
First add a reference to the System.ServiceProcess assembly.
To start:
ServiceController service = new ServiceController("YourServiceName");
service.Start();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
To stop:
ServiceController service = new ServiceController("YourServiceName");
service.Stop();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Both examples show how to wait until the service has reached a new status (running, stopped...etc.). The timeout parameter in WaitForStatus is optional.