• <bdo id='sIhzT'></bdo><ul id='sIhzT'></ul>
  • <i id='sIhzT'><tr id='sIhzT'><dt id='sIhzT'><q id='sIhzT'><span id='sIhzT'><b id='sIhzT'><form id='sIhzT'><ins id='sIhzT'></ins><ul id='sIhzT'></ul><sub id='sIhzT'></sub></form><legend id='sIhzT'></legend><bdo id='sIhzT'><pre id='sIhzT'><center id='sIhzT'></center></pre></bdo></b><th id='sIhzT'></th></span></q></dt></tr></i><div id='sIhzT'><tfoot id='sIhzT'></tfoot><dl id='sIhzT'><fieldset id='sIhzT'></fieldset></dl></div>

    <small id='sIhzT'></small><noframes id='sIhzT'>

    <tfoot id='sIhzT'></tfoot>

  • <legend id='sIhzT'><style id='sIhzT'><dir id='sIhzT'><q id='sIhzT'></q></dir></style></legend>

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

        Stop/Start service in code in Windows 7(在 Windows 7 中的代码中停止/启动服务)
            <bdo id='UEH3y'></bdo><ul id='UEH3y'></ul>

            <i id='UEH3y'><tr id='UEH3y'><dt id='UEH3y'><q id='UEH3y'><span id='UEH3y'><b id='UEH3y'><form id='UEH3y'><ins id='UEH3y'></ins><ul id='UEH3y'></ul><sub id='UEH3y'></sub></form><legend id='UEH3y'></legend><bdo id='UEH3y'><pre id='UEH3y'><center id='UEH3y'></center></pre></bdo></b><th id='UEH3y'></th></span></q></dt></tr></i><div id='UEH3y'><tfoot id='UEH3y'></tfoot><dl id='UEH3y'><fieldset id='UEH3y'></fieldset></dl></div>
          • <small id='UEH3y'></small><noframes id='UEH3y'>

            1. <tfoot id='UEH3y'></tfoot>
                1. <legend id='UEH3y'><style id='UEH3y'><dir id='UEH3y'><q id='UEH3y'></q></dir></style></legend>
                    <tbody id='UEH3y'></tbody>
                  本文介绍了在 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 中的代码中停止/启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                    <tbody id='Eu54G'></tbody>

                      • <bdo id='Eu54G'></bdo><ul id='Eu54G'></ul>

                        <small id='Eu54G'></small><noframes id='Eu54G'>

                            <tfoot id='Eu54G'></tfoot>
                            <i id='Eu54G'><tr id='Eu54G'><dt id='Eu54G'><q id='Eu54G'><span id='Eu54G'><b id='Eu54G'><form id='Eu54G'><ins id='Eu54G'></ins><ul id='Eu54G'></ul><sub id='Eu54G'></sub></form><legend id='Eu54G'></legend><bdo id='Eu54G'><pre id='Eu54G'><center id='Eu54G'></center></pre></bdo></b><th id='Eu54G'></th></span></q></dt></tr></i><div id='Eu54G'><tfoot id='Eu54G'></tfoot><dl id='Eu54G'><fieldset id='Eu54G'></fieldset></dl></div>

                            <legend id='Eu54G'><style id='Eu54G'><dir id='Eu54G'><q id='Eu54G'></q></dir></style></legend>