Starting windows services from C# MVC 4

Posted on July 6, 2018 by Suresh Kamrushi in asp.net, Windows

Sometime you need to start/stop windows services from your C# MVC application. Here i am sharing how you can do that:

1) To access windows service you need to add below namespace in your controller:

using System.ServiceProcess;

If you are getting error as “namespace name ‘ServiceController’ could not be found” while adding above namesapce you need to add using “Add Reference”. For more details see my blog HERE
2) Add below code to your controller:

ServiceController service = new ServiceController("Test Windows Service");
service.Start();

It will start your services.

  Comments or questions are welcome  

Tagged as , ,