Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Process Handle of Windows Explorer

Tags:

c#

windows

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?

like image 424
Jens Avatar asked Dec 20 '22 07:12

Jens


1 Answers

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()); 
            }
        }
like image 58
TH Todorov Avatar answered Jan 04 '23 09:01

TH Todorov