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

依然范特西╮ 提交于 2019-11-30 05:32:32

问题


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?


回答1:


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




回答2:


Another option would be to (if you don't know the HWND of the explorer window you already opened/want to reuse) enumerate and find the window with a title that "looks like" what you want, then instruct that window to come to the foreground. It wouldn't reveal the exact file you want in it, but it might be simpler than other options :)




回答3:


This is too complicated, so don't do that. Try this instead-

taskkill /f /im explorer.exe`

If you need to start it back up, then, press Ctrl+Shift+Esc, then click File, then Create new task, then type cmd, [make sure Create this task with administrative privileges is checked first!] then click OK or press Enter, then enter the following code-

start explorer.exe

Now explorer.exe must be restarted!



来源:https://stackoverflow.com/questions/4831101/how-to-re-use-existing-already-opened-windows-explorer-window-to-launch-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!