Run ServiceController.Start(serviceName) throw exception access is denied

Dani_S 3,786 Reputation points
2022-09-13T08:16:37.467+00:00

Hi,
I'm running this code from maui app lat version + worker service NET core 6
ServiceController service = new ServiceController(servicName);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
And i got:
2022-09-13 10:57:12,636 [1] ERROR AutomationClient.Shared.Helpers.ServiceHelper - StartService
System.InvalidOperationException: Cannot open 'GS Automation Client Worker Service' service on computer '.'.
---> System.ComponentModel.Win32Exception (5): Access is denied.
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
at AutomationClient.Shared.Helpers.ServiceHelper.StartService(String servicName)

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 44,816 Reputation points
    2022-09-13T10:38:47.477+00:00

    (5): Access is denied.

    A pretty clear error message.
    The user/app requires elevated (admin) permissions to start a Windows service.

    ERROR_ACCESS_DENIED =>
    https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.