Search code examples
winapiwindows-servicesstartupaccess-rights

How to start windows service from application


I have a Windows Service and i want to start it using ServiceController.The service runs as LocalSystem.When i am trying to Start it i get:

System.InvalidOperationException: 'Cannot open [service] service on computer '.'.'
Inner Exception Win32Exception:Access is denied

Main

   static void Main(string[] args) {
            using (ServiceController controller = new ServiceController("someService")) {
                controller.Start();
            }
        }

Can i somehow elevate the rights programatically in order to start the service (even if i need to use P/Invoke for Winapi ? ) .


Solution

  • You do indeed need to elevate. That requires creating a new process to start the service, either explicitly with the runas shell verb, or using the COM elevation moniker.