I have a web application written in c# mvc which is trying to check the status of windows service whether it is stopped or started. I have written the code but the issue is the code works fine on my local dev machine but when pushed to the server the code errors out stating access is denied.

I have the following code:

                using (ServiceController sc = new ServiceController("Service"))
                    if (sc.Status == ServiceControllerStatus.Running)
                        //do something
               catch (Exception ex) { }; 

The above code keeps failing at Service controller itself.

I also tried to use the below code:

    StringBuilder sb = new StringBuilder();
    Process process = new System.Diagnostics.Process();
    ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

    startInfo.FileName = @"sc";
    startInfo.Arguments = "query \"My Service\"";
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.OutputDataReceived += (sender, args) => 
    process.StartInfo.RedirectStandardOutput = true;

Both of the above code keep failing at the Service controller / SC itself.

The error is

Cannot open Service Control Manager on computer '.'. This operation might require other privileges.

Our application is running in IIS under service account and we have given admin rights and remote access rights to this account.

Not sure what else needs to be given here. Is there any specific rights to access windows service?


Try running your code as 'LocalSystem', a good background there too; https://stackoverflow.com/questions/39326284/cant-query-run-or-stop-a-windows-service

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.