Process.kill () is not allowed on Windows 7 32 bit, even with administrator privileges

Hello to all.

I got a strange problem. My application has a simple method, which if IE enters the state, if it does not respond, this method starts, closing all IE processes, 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 when debugging my application with administrator privileges, the application sometimes successfully runs this method, and sometimes I get an Access Denied error even as an administrator.

I even encoded my own manifest file, indicating that this application should run as administrator, which I think I understood.

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> 

Has anyone had the same problem? How can I fix this strange problem.

thanks

+6
c # internet-explorer process privileges
source share
4 answers

Given that you say that you always have administrator privileges when trying to call this method, the following explains why you have intermittent problems:

System.Diagnostics.Process.Kill :

If a call to the Kill method is made during process termination, a Win32Exception added to deny access.

If you quickly deleted “delete” + “OK” twice in the entry in Process-Explorer, you will know what I'm talking about.

+9
source share

Have you wrapped your code in a try / catch block to see if an exception has been 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); } 
+5
source share

This is the quirk of Internet Explorer, it starts several processes for one session. Killing one of them can lead to the exit of all of them. You probably see the exception "process exited". Do it like this:

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

you should not use "foreach" to kill it, and between two kill, you need a delay to completely destroy the process.

0
source share

All Articles