c# - How to close a specific instance of internet explorer?

Asked

Viewed 112 times

-2

I can with this code.

    foreach (Process p in Process.GetProcessesByName("explorer")) {p.Kill();}

But I need to close just one instance taking maybe by the window title.

This is the window that needs to be closed. Note: I don’t have her Handle and yes, it’s a window and not a javascript Alert or something like that.

inserir a descrição da imagem aqui

EDIT:

at the suggestion of @Bruno Warmling inserir a descrição da imagem aqui

VARIABLE RESULT P:

inserir a descrição da imagem aqui

RESULT windowtextlist

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0

Add the reference to Microsoft Internet Controls in the COM tab.

Add in usings the reference to the previously imported library.

using SHDocVw

Then do it this way:

const string janelaParaFechar = "Google";
foreach (InternetExplorer ie in new ShellWindows())
{
    if (Path.GetFileNameWithoutExtension(ie.FullName).Equals("iexplore", StringComparison.OrdinalIgnoreCase) && ie.Document.nameProp == janelaParaFechar)
    {
        ie.Quit();
    }
}

If this one doesn’t work, there’s another way. Include the functions below:

[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]

private extern static bool EnumThreadWindows(int threadId, EnumWindowsProc callback, IntPtr lParam);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]

private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);

private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

public static IList<string> GetWindowText(Process p)
{
    List<string> titles = new List<string>();
    foreach (ProcessThread t in p.Threads)
    {
        EnumThreadWindows(t.Id, (hWnd, lParam) =>
        {
            StringBuilder text = new StringBuilder(200);
            GetWindowText(hWnd, text, 200);
            titles.Add(text.ToString());
            return true;
        }, IntPtr.Zero);
    }
    return titles;
}

Then use as follows:

string janelaParaFechar = "Google - Internet Explorer";
foreach (Process process in Process.GetProcessesByName("iexplore"))
{
    var windowTextList = GetWindowText(process);
    if (windowTextList?.Count > 0 && windowTextList.Contains(janelaParaFechar))
    {
        process.Kill();
    }
}
  • But then you are using Edge and not internet explorer?

  • use the c# webbrowser and it uses IE

  • I’ve included one more way to do it

  • windowTextList comes with 73 items but none is the tab that has to be closed

  • and only one process returns for you?

  • yes there is only this IE window open, the process is returning right

  • I will edit the question and put the results that came

  • @Miltonmachadopereira but because you’re trying to do it by killing the process, if you have one WebBrowser? That application with the WebBrowser is yours? you have control over her programming??

  • Because this error window opens in a new window, then I lose control over it, it would only work differently if I could open the error window in a new webbrowser.

  • edited the question

  • Dude, the process has to be iexplore and not explorer. Explorer is the Windows window manager and not Windows Explorer. Try to change there

  • hence no process comes

  • 1

    But then you must enter the name of the host process of this window... and nay the explorer.

  • worse than I think this window has no process or is a child process, which does not appear in the task manager

  • I managed with windows findWindowByCaption... taking the text "The following error occurred: -- Web Page dialog box" which is in the title of the window

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.