views:

32

answers:

1

I try to close my browser in C# using the following code:

private void KillIE()
    {
        ShellWindows winShells = new ShellWindowsClass();
        foreach (InternetExplorer Browser in winShells )
        {
            Browser.Quit();
        }
    }

Sometimes its works but today it doesn't and I cant get it to work. Now all I get is the following message.

Error HRESULT E_FAIL has been returned from a call to a COM component.

Anyone have any clue why this is happening?

A: 

To use the Process Class might be a workaround to that, and it's definitely the '.NET way' of doing it.

using System.Diagnostics;

foreach (Process proc In Process.GetProcessesByName("iexplorer.exe"))
{
    // The nice way
    proc.CloseMainWindow();

    // The hard way
    proc.Kill();
}
Bobby