5

i want to turn windows service on when it is off. is it possible to make via code from web application with c#? i am using asp.net mvc and c#.

r.r
  • 7,023
  • 28
  • 87
  • 129

4 Answers4

7

You're looking for the ServiceController class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • and here is some example http://codebetter.com/brendantompkins/2004/06/14/windows-service-administration-with-net-part-3-controlling-your-service/ – Silx Mar 08 '11 at 14:33
6

Its possible to do it but its unlikely that you want to run your website under an account which has enough rights to be able to Start/Stop services. You can use the ServiceController class to start a service see here

SecretDeveloper
  • 3,140
  • 2
  • 31
  • 36
2

Here an example:

                var sc = new ServiceController("Your service name");
                sc.Stop();
                sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(120));
                logger.Info("service stopped.");
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
1

Use the ServiceController class.

CheeZe5
  • 975
  • 1
  • 8
  • 24