在 Windows 7 中的代码中停止/启动服务

Stop/Start service in code in Windows 7(在 Windows 7 中的代码中停止/启动服务)
本文介绍了在 Windows 7 中的代码中停止/启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个应用程序和服务来监视一组给定的服务,并且 a) 确保它们正在运行 b) 根据某些标准,根据需要重新启动它们.

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(); 
       }            
   }   

我明白了:

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.

任何帮助将不胜感激.

推荐答案

诊断:

UAC 确实是问题所在.您的应用程序需要提升的用户权限.这就是以管理员身份运行"在 IDE 之外工作的原因.

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

一些网站建议禁用 UAC.由于这在我的环境中是不可能的,我决定通过代码请求提升的用户权限.Wikipedia 提供了我需要的帮助.请求提升"部分提供了解决方案.

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.

简而言之解决方案:

编辑您的应用程序清单文件以反映您的要求.

Edit your application manifest file to reflect your requirement.

1.1.右键单击您的项目
1.2.点击属性"
1.3.选择应用程序"选项卡 - 默认页面
1.4.单击查看 UAC 设置" - 此按钮打开应用程序清单文件 (app.manifest)
1.5.查找UAC 清单选项"部分
1.6.删除或评论当前条目 - {requestedExecutionLevel level="asInvoker" uiAccess="false"}
1.7.更改为 {requestedExecutionLevel level="requireAdministrator" uiAccess="false"}.MS 提供您的 3 个选项作为部分注释的一部分.

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.

这篇关于在 Windows 7 中的代码中停止/启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)