开发者

Stop/Start service in code in Windows 7

开发者 https://www.devze.com 2022-12-08 17:51 出处:网络
I am trying to write a app and service which monitor a given set of services and a) makes sure they are running and b) based on certain criteria, restart them as needed.

I am trying to write a app and service which monitor a given set of services and a) makes sure they are running and b) based on certain criteria, restart them as needed.

I keep running into an access denied error.

If I simply iterate through the processes on the system, find the one I want like so:

foreach (ServiceController sc in ServiceController.GetServices())
   {                
       if(sc.ServiceName == "MyServiceName")
       {
            sc.Stop();
            sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 60));
            sc.Start(); 
       }            
   }   

I get:

InnerException: System.InvalidOperationException
        Message="Cannot open My Service service on computer '.'."
        Source="System.ServiceProcess"
        StackTrace:
             at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
             at System.ServiceProcess.ServiceController.Stop()
             at lib.ListServices() in D:\lib.cs:line 552
             at lib.Init() in D:\lib.cs:line 56
   InnerException: System.ComponentModel.Win32Exception
             Message="Access is denied"
            开发者_开发技巧 ErrorCode=-2147467259
             NativeErrorCode=5
             InnerException: 

I have tried to impersonate a user, I have tried to do the same code from another service which is running as a system service. Neither of which have actually been able to affect the service. If its started, I cannot stop it. If its stopped, I cannot start it. I know this is all related to permissions I'm just not finding a mechanism that actually lets me control the service.

Any assistance would be greatly appreciated.


Diagnoses:


UAC is indeed the problem. Your application requires elevated user rights. That is why the "run as administrator" works outside of the IDE.

Some sites suggest disabling UAC. As this is not a possibility in my environment, I decided to request elevated user right via code. Wikipedia provided the help I needed. The "Requesting elevation" section provides the solution.

The solution in short:


Edit your application manifest file to reflect your requirement.


1.1. Right click your project
1.2. Click "Properties"
1.3. Select "Application" tab - default page
1.4. Click "View UAC Settings" - This button opens the application manifest file (app.manifest)
1.5. Look for the "UAC Manifest Options" section
1.6. Remove or comment the current entry - {requestedExecutionLevel level="asInvoker" uiAccess="false"}
1.7. Change to {requestedExecutionLevel level="requireAdministrator" uiAccess="false"}. MS provides your 3 options as part of the section comment.


I think this is UAC fault... Try to run the exe with right click and "Run as Administrator". If that helps, you might want to add a Manifest file to your executable project with level="requireAdministrator".


Could it be the .net security policy that is preventing your app from controlling the service?

According to MSDN, your assembly needs full trust for this to work.


You have to set the application to run as administrator. If you look under the files properties menu. On the compatibility tab, there is a Privilige Level option, to let the program run as administrator. It worked with my own system trays in windows 7.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号