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?
– Bruno Warmling
use the c# webbrowser and it uses IE
– Milton Machado Pereira
I’ve included one more way to do it
– Bruno Warmling
windowTextList comes with 73 items but none is the tab that has to be closed
– Milton Machado Pereira
and only one process returns for you?
– Bruno Warmling
yes there is only this IE window open, the process is returning right
– Milton Machado Pereira
I will edit the question and put the results that came
– Milton Machado Pereira
@Miltonmachadopereira but because you’re trying to do it by killing the process, if you have one
WebBrowser
? That application with theWebBrowser
is yours? you have control over her programming??– Bruno Warmling
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.
– Milton Machado Pereira
edited the question
– Milton Machado Pereira
Dude, the process has to be
iexplore
and notexplorer
. Explorer is the Windows window manager and not Windows Explorer. Try to change there– Bruno Warmling
hence no process comes
– Milton Machado Pereira
But then you must enter the name of the host process of this window... and nay the
explorer
.– Bruno Warmling
worse than I think this window has no process or is a child process, which does not appear in the task manager
– Milton Machado Pereira
I managed with windows findWindowByCaption... taking the text "The following error occurred: -- Web Page dialog box" which is in the title of the window
– Milton Machado Pereira