4
votes

I want to get the Handle of my "Windows Explorer" Windows (not Internet Explorer).

Normally it works with

var processes = Process.GetProcesses();
foreach (var process in processes)
{
    var handle = process.Handle;
}

What i want to do is following:

Bring the a specific Explorer Window to ForeGround. I have implemented the "ToForeGround" Method and it works fine for all other Windows except the Windows Explorer

But with the Windows Explorer i only get the Process of the Taskbar independent of how much Windows are open, there is only one "Windows Explorer" Process.

Or can somebody explain me why the "Windows Explorer" is different from other Programms?

3
What will you do with this handle? This is almost certainly part of a larger problem/solution and, almost certainly, this step is wrong - but without knowing what the larger context is, I can't really point you towards a better solution. So what are you actually trying to do?Damien_The_Unbeliever
I "only" want to bring it into foreground. I am programming sth like Alt + Tab. And i know that my solution is not working..Jens
Which explorer window are you trying to get access to? I believe you will be sending this handle to SetForegroundWindow eventually.danish
The SetForeground is not the Problem. that is working. I need a datatype to hold the windows, so i can call them to foreground when needed. So i need access to all explorer windowsJens
So, it's not so much that you need the process handle, more that you want to get (all, or some, in which case, which ones?) of the window handles?Damien_The_Unbeliever

3 Answers

5
votes

Point well taken, so let me try to explain briefly what the code does - you can read more about the ShellWindows object here. The code below helps you find all running instances of Windows Explorer (not Internet Explorer, note that "explorer" is used in the if statement and not "iexplore").

Add Reference to Shell32.dll, located in the Windows/system32 folder

        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

        string filename;
        ArrayList windows = new ArrayList();

        foreach (SHDocVw.InternetExplorer ie in shellWindows)
        {
            filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
            if (filename.Equals("explorer"))
            {
                //do something with the handle here
                MessageBox.Show(ie.HWND.ToString()); 
            }
        }
3
votes

can somebody explain me why the "Windows Explorer" is different from other Programms?

It's the default shell. Explorer.exe handles many (user interface) tasks of Windows, some of which are the taskbar, hosting extensions and harboring the file explorer.

It's a (sort-of) single-instance process, so when you launch a new instance, it'll hand the parameters to the running instance.

If you want to focus or open an Explorer at a certain path, just use:

Process.Start(@"C:\SomeFolder\");
3
votes

Following code iterates through all explorer and internet explorer windows(tabs) (W7/IE11). Location URL will give the folder that is being viewed in the explorer. If the folder is the one you need to bring to foreground, you can use HWND for that window and bring it to foreground.

Note location URL for explorer window for "Computer" will be blank. I am not sure if there are more special cases like that.

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

foreach (SHDocVw.InternetExplorer window in shellWindows){
    if (window.LocationURL.Contains("Some Folder I am interested in")){
        SetForegroundWindow((IntPtr)window.HWND);
    }
}