Jens
Jens

Reputation: 2702

Get Process Handle of Windows Explorer

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?

Upvotes: 4

Views: 6454

Answers (3)

TH Todorov
TH Todorov

Reputation: 1159

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()); 
            }
        }

Upvotes: 5

danish
danish

Reputation: 5600

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);
    }
}

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151584

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\");

Upvotes: 3

Related Questions