Process.kill() denied in Windows 7 32bits even wit

2019-01-28 00:57发布

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

4条回答
Melony?
2楼-- · 2019-01-28 01:20

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 {}
        }
查看更多
聊天终结者
3楼-- · 2019-01-28 01:21

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

查看更多
你好瞎i
4楼-- · 2019-01-28 01:41

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.

查看更多
何必那么认真
5楼-- · 2019-01-28 01:41

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);
}
查看更多
登录 后发表回答