Monday, February 25, 2013

How to Start/Stop Service using Coding?



ServiceController controller = ServiceController.GetServices().Where(s => s.ServiceName == "ServiceName").FirstOrDefault();
if (controller == null)
{
      MessageBox.Show("Service is missing.");
      return;
}
if (controller.Status == ServiceControllerStatus.Stopped)
{
     controller.Start();
}
else if (controller.Status == ServiceControllerStatus.Running)
{
     controller.Stop();
 }