This is a mirror of official site: http://jasper-net.blogspot.com/

How to start or stop a Windows Service using C#

| Thursday, August 5, 2010
We can start, stop, pause, continue and refresh a service using Start, Stop, Pause, Continue, and Refresh methods.  Close method disconnects this ServiceController instance from the service and frees all the resources that the instance allocated.

The following code snippet checks if a service is stopped, start it; otherwise stop it.

Let's say, you have a service named "MyServiceName". First you create a ServiceController object and then call its Start or Stop methods to start and stop a windows service.


ServiceController service = new ServiceController("MyServiceName");
if ((service.Status.Equals(ServiceControllerStatus.Stopped)) ||
   (service.Status.Equals(ServiceControllerStatus.StopPending)))
   service.Start();
else    service.Stop();


ServiceController class also have Pause, Continue, and Refresh methods to pause, continue, and refresh a windows service.

Read more: C# Corner

Posted via email from .NET Info

0 comments: