I want to disable the windows service when it is off. Is it possible to do through code from a web application using C #? I am using asp.net mvc and c #.
You are looking for the ServiceController class.
ServiceController
It can be done, but it is unlikely that you want to run your website under an account that has sufficient rights to be able to start / stop services. You can use the ServiceController class to start the service; see here.
Here is an example:
var sc = new ServiceController("Your service name"); sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(120)); logger.Info("service stopped.");
Use the ServiceController class.