.net ServiceController - Unable to open the service name of the service on the machine.

I'm trying to access a Windows service (also created by me) through a WinForms application using the ServiceController :

service = new System.ServiceProcess.ServiceController("MyService")

      

The service returns OK and I see the status through myservice.Status

.

But when I try to start (or stop) it returns an error:

Unable to open MyService on the machine. '

I am an Administrator and the service process runs as a NetworkService.

I am new to .net and windows apps and I cannot debug it anymore.

+2


source to share


2 answers


I was finally able to solve the problem by setting the runlevel in the app.manifest manifest (project-> new item-> application)

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

      

The management application now requires administrator privileges and therefore can start / stop the service without issue.

However, I believe I can do it in a different way, using impersonating code, or perhaps downgrading in some way.



I will have to revisit this before running the application and I will post my results here.

Thanks to Drew for the debugging guide.

Hello

+2


source


Are you sure you are using the service name , not the name?

Double check that you are the local administrator in the box.

Is the service running on a local machine? I'm not sure why you are seeing the hostname. "

EDIT:



I read online that the following code calls InvalidOperationException

if Test Service

not a valid service name:

var sc = new ServiceController("Unknown Service");
string name = sc.DisplayName;

      

The documentation for this constructor does not indicate that this will throw an exception, so I assume you will need to use the service name. Any exception you see after this is actually a real error.

Hope it's clearer.

+1


source







All Articles