Monday, June 14, 2010

Start and stop windows services programmatically by using C#.NET

This short code snippet shows how you can start and stop windows services and also how to find the current status of any windows services.

System.ServiceProcess.ServiceController class

You can use the .NET class System.ServiceProcess.ServiceController to work with the windows services.

Include the namespace in the top of your class:

using System.ServiceProcess;

Create an instance of the class:

ServiceController controller = new ServiceController();

See the following code which gets the status of the IIS Admin service. Also, you can start and stop the service using this class.

controller.MachineName = ".";
controller.ServiceName = "IISADMIN";
string status = controller.Status.ToString();

// Stop the service

// Start the service

No comments:

Post a Comment