Search code examples
c#internet-explorerforceclose

Close IE specific window with C# using Kill is not working


I am trying to close the most recent window/tab of IE but when I call the Kill method the window relaunches without the content of the page. This is the code that I use to get the most recent IE process:

var a = System.Diagnostics.Process.GetProcessesByName("iexplore");
DateTime earliestStart = DateTime.Today.Subtract(new TimeSpan(1,0,0,0));
System.Diagnostics.Process youngestProccess = a.FirstOrDefault();


                foreach(var b in a){
                    if (b.StartTime > earliestStart)
                    {
                        earliestStart = b.StartTime;
                        youngestProccess = b;
                    }
                }                
                youngestProccess.Kill();

The code is working in the way that the most recent window "stop" working but the window is not beeing closed

Any idea?


Solution

  • Hi I just discover how to solve the problem.

    The message is being thrown because my IE has the option "Enable automatic crash recovery" checked in the Internet Options Advanced tab.

    So if you face this you have 2 options: un-check that option forever (which might work for your case) or like in my case you can change the selection via registry keys and when you finish your testing return the value to be on.

    So to do this you need to add this to your code before opening IE.

    Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Recovery", "AutoRecover", 2);  
    

    To turn back on the option you must do the same but with a 0

    Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Recovery", "AutoRecover", 0);