2

I'm trying to start a service with C # but throws me the following error:

ServiceController service = new ServiceController(sServiceName);
service.MachineName = localComputerName;
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);

System.InvalidOperationException: Cannot open service on computer System.ComponentModel.Win32Exception: Access is denied at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at System.ServiceProcess.ServiceController.Stop()

Yuck
  • 49,664
  • 13
  • 105
  • 135
gemr1423
  • 739
  • 2
  • 11
  • 33

4 Answers4

7

Look for your permissions. May me you need to run your app (or Visual Studio) from Administrator account.

Sergey Shulik
  • 950
  • 1
  • 9
  • 24
3

Your Access is denied error indicates to me that the identity under which your process / application is running does not have permission to start & stop services.

Yuck
  • 49,664
  • 13
  • 105
  • 135
1

Have you red the exception text? you simply have no permission to start a server. Read: http://msdn.microsoft.com/en-us/library/h846e9b3.aspx ( http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontrollerpermission.aspx specifically) or simply launch the application as an administrator

Polity
  • 14,734
  • 2
  • 40
  • 40
0

You may want to check out this question for some help on permissions when starting your service.

Community
  • 1
  • 1
Zann Anderson
  • 4,767
  • 9
  • 35
  • 56