none
Closing IE in C#

    Question

  •  

    Hello I am writting a little project to just learn the more hidden features of c#. Things like your systems current statis ect.

     

    The thing I am stumped on and cannot seem to find anywhere how to do it is to obtain an instance of the Internet Explorer windows open on the current machine and then close them. If anyone has any idea how to do this or even the namespace to look it would be greatly appreciated.

     

    I know it is the simple line "System.Diagnostics.Process.Start(www.hotmail.com);" to open a website but unfortuantely there is not the reverse of that like Process.Stop(...); sooo ya, any thoughts would be great.

    Thank you in advance;

    XXChester

    Wednesday, October 10, 2007 2:25 PM

Answers

  • This might not be exactly what you want. But if you know the MainWindowTitle, you could do something like the following:

     

    // Start ieexplorer.exe and go to www.google.com.

    System.Diagnostics.Process.Start("www.google.com");

    // Waite 10 seconds.

    System.Threading.Thread.Sleep(10000);

    // Get all IEXPLORE processes.

    System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");

    foreach (System.Diagnostics.Process proc in procs)

    {

        // Look for Google title.

        if (proc.MainWindowTitle.IndexOf("Google") > -1)

            proc.Kill(); // Close it down.

    }


    • Marked as answer by XXChester Monday, May 25, 2009 3:19 PM
    Thursday, October 11, 2007 6:59 PM

All replies

  • The reason is because there might be multiple processes with "iexplorer.exe". Do you end the first? the last? all of them?

    The way you get access to stopping/killing a process remotely from C# is to get an instance of the System.Diagnostics.Process and calling the instance methods. That way it knows which process to stop/terminate.

    So look into how to get instances from current running processes (It's another static method of Process).
    Wednesday, October 10, 2007 4:09 PM
  • This might not be exactly what you want. But if you know the MainWindowTitle, you could do something like the following:

     

    // Start ieexplorer.exe and go to www.google.com.

    System.Diagnostics.Process.Start("www.google.com");

    // Waite 10 seconds.

    System.Threading.Thread.Sleep(10000);

    // Get all IEXPLORE processes.

    System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");

    foreach (System.Diagnostics.Process proc in procs)

    {

        // Look for Google title.

        if (proc.MainWindowTitle.IndexOf("Google") > -1)

            proc.Kill(); // Close it down.

    }


    • Marked as answer by XXChester Monday, May 25, 2009 3:19 PM
    Thursday, October 11, 2007 6:59 PM
  • If you just need the function to close IE.

     

    try the following

     

                // Initialize the IE com object 
                SHDocVw.InternetExplorerClass ieCom = new SHDocVw.InternetExplorerClass();
               

                // Show ie window or not
                ieCom.Visible = true;

     

                //Close the IE
                base.ieCom.Quit();

     

    Ricky.

     

    Monday, October 15, 2007 12:41 AM
  • I like Ricky Wang's COM example to open and close IE. Also you have more control over IE.
    Wednesday, October 17, 2007 6:34 AM
  • Hello Chester,

     

    I want to develop a plug-in for IE. Would you recommend me using C# for this purpose?

     

    Thanks in advance!

    Regards

     

     

    Friday, October 26, 2007 5:02 PM
  • I realize this thread is over a year and a half old but I did not see your Question Bortecin and for anyone else that looks at this thread I cannot comment on your question Bort as I have never written a plugin before. The program I wrote was just a learning experience on how to use some of the built in classes of C#. After that I went on into test driven development and wrote my own framework to find controls on a web page (yes I know this has been done many times but the best way to learn is to do it yourself).
    Monday, May 25, 2009 3:18 PM
  • if you don't mind can you send me a sample like opening a few tabs in chrome but only closing a specific tab. thanks. my email leon_ken92@hotmail.com
    Tuesday, July 30, 2013 5:41 AM