Exception: Cannot open Servicexxx service on computer 'COMPUTER_NAME'
-
I used below code to start service.
var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
if (sc.Status.ToString().Equals("Stopped"))
sc.Start();Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance
-
I used below code to start service.
var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
if (sc.Status.ToString().Equals("Stopped"))
sc.Start();Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance
NarVish wrote:
if (sc.Status.ToString().Equals("Stopped"))
Correct way to do the check is
if (sc.Status == ServiceControllerStatus.Stopped) {
sc.Start();
}NarVish wrote:
I would like to ask for credentials before the user tries to start service.
Read about Application manifest and UAC[^]. If you need administrator privilege only for starting the service, you could delay request for elevation till you need it. This can be done by starting a new process and setting ProcessStartInfo.Verb[^] to
runas
. This new process can execute the code to start/stop the service. You need the new process because you can't change elevation levels of the current process while it is running.Best wishes, Navaneeth
-
I used below code to start service.
var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
if (sc.Status.ToString().Equals("Stopped"))
sc.Start();Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance
If you're going to be asking for a username and password, what's the problem?? It's two labels and two textboxes on a small form and showing it!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak