To Start and Stop Windows Service Programatically.

Here is a code sample to start and stop a windows service programmatically
using this we can know the status of a windows service wether it is in start mode or stop mode

This is to start a windows service:


try
{
ServiceController service = new ServiceController("NarenService", "VNREDDY");
if (service.Status == ServiceControllerStatus.Running)
{
MessageBox.Show("SERVICE ALREADY IN RUN STATUS ONLY....");
return;
}

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}


this is to stop a windows service.


try
{
ServiceController service = new ServiceController("NarenService", "VNREDDY");
if (service.Status == ServiceControllerStatus.Stopped)
{
MessageBox.Show("SERVICE ALREADY IN STOP STATUS....");
return;
}
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: