Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-use existing/already-opened Windows Explorer window to launch Explorer

I have an application that makes frequent use of launching explorer.exe. I would like to re-use existing/already-opened explorer windows instead of creating a new one each time I start the process.

Here is what my code looks like:

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo
{
    UseShellExecute = true,
    FileName = "Explorer.exe",
    Arguments = myDirectoryPath
};
System.Diagnostics.Process.Start(info);

I didn't see a command-line switch to do this. One approach I tried was to simply kill any 1 existing explorer process and replace it with a new one:

var processes = System.Diagnostics.Process.GetProcesses(Environment.MachineName);int kills = 0;
for (int i = 0; i < processes.Length; i++)
{
    System.Diagnostics.Process p = processes[i];
    if (p.ProcessName == "explorer" && kills < 1)
        ++kills
    p.Kill();
}

But this results in the unwanted effect of not just killing 1 process, but killing explorer completely so that even the taskbar disappears.

So, how do you use an existing Explorer window, if one exists, to start Explorer?

like image 397
T. Webster Avatar asked Jan 28 '11 17:01

T. Webster


1 Answers

The IShellWindows COM interface will give you a list of open explorer windows, you can get and set the address of any explorer window, see this blog entry for a C++ sample. I doubt .NET has a native implementation of this, so you probably need to PInvoke

like image 97
Anders Avatar answered Sep 23 '22 22:09

Anders