Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceController Exception (Cannot control service on computer '.'.)

Tags:

c#

windows

I am trying to send a command to my service TestService from a program running as administrator, I am able to start/stop it just fine, but whenever I try to ExecuteCommand() I encounter an Exception:

Cannot control service on computer '.'.

        try
        {
            System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("TestService");
            
            if (service.Status == ServiceControllerStatus.Stopped)
            {
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running);
            }

            service.ExecuteCommand(100); // Causes Exception every time
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            throw e;
        }
like image 892
le palson Avatar asked Apr 10 '26 08:04

le palson


1 Answers

This is happening is because ExecuteCommand only accepts integers: 128-256, anything under 128 is system reserved and since 100 is being passed the Exception occurs.

Replacing service.ExecuteCommand(100) with something like service.ExecuteCommand(130) will work just fine.

like image 83
le palson Avatar answered Apr 11 '26 22:04

le palson