即使具有管理员权限,Process.kill() 在 Windows 7 32 位中也会被拒绝

发布于 2024-09-15 02:29:15 字数 1411 浏览 8 评论 0原文

大家好。

我遇到了一个奇怪的问题。我的应用程序有一个简单的方法,如果 IE 进入无响应状态,则会触发此方法并关闭所有 IE 进程,然后应用程序重新启动 IE 的工作。

方法代码:

foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())
{
     if (exe.ProcessName.StartsWith("iexplore"))
          exe.Kill();
}

即使使用管理员权限调试我的应用程序,应用程序有时也会成功运行此方法,而有时即使以管理员身份运行,我也会收到错误访问被拒绝

我什至编写了自己的清单文件,指定需要使用管理员权限执行此应用程序,我认为我做对了。

清单代码:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="Demo.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</asmv1:assembly>

以前有人遇到过同样的问题吗?我该如何纠正这个奇怪的问题。

谢谢

Hello every one.

I'm faced with a weird problem. My application has a simple method that in case IE enters a state were it gets unresponsive this method is fired closing all IE process's and then the application restarts its work with IE.

Method code:

foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())
{
     if (exe.ProcessName.StartsWith("iexplore"))
          exe.Kill();
}

Even debugging my application with Administrator privileges the application sometimes runs this method successfully and some other times i get the error Access Denied even running as Administrator.

I even coded my own manifest file specifying the need for this application to be executed with Administrator rights, which i think i got it right.

Manifest Code:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="Demo.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</asmv1:assembly>

Anyone had this same issue before? How can i correct this weird problem.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送你一个梦 2024-09-22 02:29:15

鉴于您声称在尝试调用此方法时您始终拥有管理权限,以下内容将解释为什么您会遇到间歇性问题:

System.Diagnostics.Process.Kill

<块引用>

如果在进程当前终止时调用 Kill 方法,则会抛出 Win32Exception 以表示拒绝访问。

如果您在 Process-Explorer 中的某个条目上快速点击“删除”+“确定”两次,您就会知道我在说什么。

Given that you say you always possess administrative privileges when you attempt to call this method, the following would explain why you have intermittent issues:

System.Diagnostics.Process.Kill:

If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

If you've quickly hit 'delete' + 'OK' twice, on an entry in Process-Explorer, you'll know what I'm talking about.

司马昭之心 2024-09-22 02:29:15

您是否将代码包装在 try/catch 块中以查看是否引发异常?如果没有,请尝试

try
{            
    foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())   
    {   
        if (exe.ProcessName.StartsWith("iexplore"))   
            exe.Kill();   
    } 
}

catch (Win32Exception e)
{
    Console.WriteLine("The process is terminating or could not be terminated.");
}

catch (InvalidOperationException e)
{
    Console.Writeline("The process has already exited.");
}

catch (Exception e)  // some other exception
{
    Console.WriteLine("{0} Exception caught.", e);
}

Did you wrap your code in a try/catch block to see if an exception was thrown? If not, try

try
{            
    foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())   
    {   
        if (exe.ProcessName.StartsWith("iexplore"))   
            exe.Kill();   
    } 
}

catch (Win32Exception e)
{
    Console.WriteLine("The process is terminating or could not be terminated.");
}

catch (InvalidOperationException e)
{
    Console.Writeline("The process has already exited.");
}

catch (Exception e)  // some other exception
{
    Console.WriteLine("{0} Exception caught.", e);
}
场罚期间 2024-09-22 02:29:15

这是 Internet Explorer 的一个怪癖,它会为一个会话启动多个进程。杀死其中一个可以导致他们全部退出。您可能会看到“进程已退出”异常。这样做:

        foreach (var ie in Process.GetProcessesByName("iexplore")) { 
            try {
               ie.Kill();
            }
            catch {}
        }

It is a quirk of Internet Explorer, it starts multiple processes for one session. Killing one of them can cause all of them to exit. You probably see the "process has exited" exception. Do it like this instead:

        foreach (var ie in Process.GetProcessesByName("iexplore")) { 
            try {
               ie.Kill();
            }
            catch {}
        }
漫雪独思 2024-09-22 02:29:15

你不应该使用“foreach”来杀死它,并且在两次杀死之间,你需要延迟才能完全杀死进程

you shouldn't use "foreach" to kill it, and between two kill, you need delay for the process to be killed completely

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文