4

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?

Jens
  • 2,592
  • 2
  • 21
  • 41
  • 1
    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 Jan 09 '15 at 07:40
  • 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 Jan 09 '15 at 07:43
  • Which explorer window are you trying to get access to? I believe you will be sending this handle to SetForegroundWindow eventually. – danish Jan 09 '15 at 08:51
  • 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 windows – Jens Jan 09 '15 at 08:55
  • 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 Jan 09 '15 at 08:56
  • It's perfectly normal for a single process to create multiple top level windows. Is that the source of confusion for you? – David Heffernan Jan 09 '15 at 09:00
  • i don't care about more windows on one process at the moment. maybe i will add something like that later. maybe i also don't need the handle. the only thing i want to do is to bring an explorer window to foreground. – Jens Jan 09 '15 at 09:04

3 Answers3

5

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()); 
            }
        }
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
TH Todorov
  • 1,129
  • 11
  • 26
  • 2
    "Try this" is not a good answer. Explain what the code does. And certainly don't use ArrayList, or we know you copied this code from a site from the 2000's. – CodeCaster Jan 09 '15 at 09:02
  • I used try since I am not certain what the OP wants to do, hence the solution might or might not work for him, here's a link to an article that again might or might not help him http://omegacoder.com/?p=63 – TH Todorov Jan 09 '15 at 09:06
  • 1
    If you don't understand what the code does, you might want to refrain from answering, and posting a link as comment like "does this help?". – CodeCaster Jan 09 '15 at 09:08
  • I have seen both articles. But i do not understood what is going there. My Questions would be: To the SHDocVw Version: It seems that this is doing what i want for the Internet Exploere?! How to apply it to the Windows Explorer? An i remember that i tried this, but has not worked.. To Omegacode: when i remember right then the Code is C/C++ Code. How can i use that in my C# programm? – Jens Jan 09 '15 at 09:11
  • it helps you find the handle of each Windows Explorer instance, not Internet Explorer – TH Todorov Jan 09 '15 at 09:35
  • 1
    to CodeCaster - the article explains what the code does, if you are having a hard time comprehending it, you might want to drop the attitude and ask for help. – TH Todorov Jan 09 '15 at 09:57
  • I'm not having a hard time understanding this code (I can read and I very well know the API this code uses) nor am I "having an attitude", I'm giving you hints on **how to improve your answer**. Answers should be self-contained, not contain a link where it is explained. You also might want to use @notification instead of "To", so people get notified of your response. – CodeCaster Jan 09 '15 at 10:37
3

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\");
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Ok thank you for the answer! I will read the article about the Shell later, but i think your answer is nice and helps me to understand the more – Jens Jan 09 '15 at 08:57
  • I know Process.Start(@"C:\SomeFolder\"); but that is not what i want :/ It's another usecase i could implement into my programm, but the actual one is to be able to do sth with an open window – Jens Jan 09 '15 at 08:59
3

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);
    }
}
danish
  • 5,550
  • 2
  • 25
  • 28