简体   繁体   English

使用 Process.Start 打开 Explorer window 创建过多的 explorer.exe 进程

[英]Open an Explorer window with Process.Start creates excessive explorer.exe processes

When I start an explorer window with a directory path, a new explorer.exe process is spawned.当我使用目录路径启动资源管理器 window 时,会生成一个新的 explorer.exe 进程。 After my program exits the explorer.exe process stays open which is ok, but when I close that explorer window the process doesn't close.在我的程序退出后,explorer.exe 进程保持打开状态,这没关系,但是当我关闭该 explorer window 时,该进程不会关闭。 If I spawned several explorer windows in my program, I end up with many explorer.exe process that stay around even when all the explorer windows are closed.如果我在我的程序中生成了几个资源管理器 windows,我最终会得到许多 explorer.exe 进程,即使所有资源管理器 windows 都已关闭。

ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe", folderPath);
Process.Start(startInfo);

在此处输入图像描述

However, if I were to start just the explorer.exe with no 2nd argument, an extra explorer.exe process will not be created even though an explorer window is opened.但是,如果我只启动没有第二个参数的 explorer.exe,即使打开了 explorer window,也不会创建额外的 explorer.exe 进程。 When that explore window is closed, you also don't see any extra explorer.exe process other than the desktop process one.当 explore window 关闭时,除了桌面进程之一之外,您也看不到任何额外的 explorer.exe 进程。 Please note that my desktop explore.exe is PID 7704 is this example and that should be the only one running.请注意,我的桌面 explore.exe 是 PID 7704 是这个示例,应该是唯一一个正在运行的示例。

ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe", folderPath);
Process.Start(startInfo);

在此处输入图像描述

I found that this is true if I manually do this launching using cmd.exe.我发现如果我使用 cmd.exe 手动启动,这是真的。 In the command prompt, if you run "explorer.exe C:/", you can create this same "dangling" explorer.exe process while running just "explorer.exe" doesn't.在命令提示符下,如果您运行“explorer.exe C:/”,则可以创建相同的“悬空” explorer.exe 进程,而只运行“explorer.exe”则不会。

So my question is how do I open an explorer window for the folder I want without creating all these extra explorer.exe processes.所以我的问题是如何在不创建所有这些额外的 explorer.exe 进程的情况下为我想要的文件夹打开资源管理器 window。 I also wonder whether the "dangling" explorer.exe processes would eventually go away on their own or they would sit there and hog all the memory until I have to force end task.我还想知道“悬空”的 explorer.exe 进程是否最终会自行离开 go 或者他们会坐在那里并占用所有 memory 直到我不得不强制结束任务。 So far I have seen them persisted for days and never going away even though there are no explorer windows associated with them anymore after the windows are closed.到目前为止,即使在 windows 关闭后不再有与它们相关的资源管理器 windows ,我也看到它们持续了好几天并且从未消失。

Only thing I can think of so far is to open an explorer.exe with no arguments so it doesn't create the extra process then somehow figure out a way to find that open explorer window handle then use it to browse to the directory I want.到目前为止,我唯一能想到的就是打开一个没有 arguments 的 explorer.exe,这样它就不会创建额外的进程,然后以某种方式找到打开的 explorer window 句柄然后用它浏览到我想要的目录. I have seen some DLLImport("user32.dll") that might be able to do this.我见过一些 DLLImport("user32.dll") 可能能够做到这一点。

It sounds much more difficult than just using the Process.Start to get to the folder I want right away.这听起来比只使用 Process.Start 来立即访问我想要的文件夹要困难得多。 I just don't think it's a good idea for my program to leave a lot of memory drain processes behind even though it's something you can already do with cmd.exe.我只是不认为我的程序留下很多 memory 排放过程是一个好主意,即使它已经可以用 cmd.exe 来完成。 I don't know whether this is a Windows "feature" that eventually ends in an out of memory situation.我不知道这是否是 Windows “功能”最终以 memory 的情况结束。

Here is a solution which creates no additional explorer.exe processes, and uses the existing one as it seems.这是一个解决方案,它不创建额外的 explorer.exe 进程,并使用现有的进程。

I realized this when opening the folder from a command shell.从命令 shell 打开文件夹时,我意识到了这一点。 When run "explorer c:\temp", a new explorer.exe is created.运行“explorer c:\temp”时,会创建一个新的 explorer.exe。 But when run "start c:\temp" ..但是当运行"start c:\temp" ..

So the following code starts a hidden cmd process, feeds its input with "start <folder>\r\n" , and "exit\r\n" afterward to close it.所以下面的代码启动一个隐藏的 cmd 进程,用"start <folder>\r\n"输入它的输入,然后用"exit\r\n"关闭它。 The code also checks if the new cmd process is leaked.该代码还会检查新的 cmd 进程是否泄漏。

Hope this helps.希望这可以帮助。

Process cmd = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "cmd",
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardInput = true
    }
};
cmd.Start();
int processId = cmd.Id;
Debug.WriteLine("PID: {0}", processId);
cmd.StandardInput.Write("start " + folderPath + "\r\n");
cmd.StandardInput.Write("exit\r\n");
cmd = null;

try
{
    Process isCmdStillThere = Process.GetProcessById(processId);
}
catch (Exception errorQueryingProcess)
{
    Debug.Assert(errorQueryingProcess.Message == "Process with an Id of " + processId + " is not running.");
}

I just happened to have done something like this just now and maybe helped我刚刚碰巧做了这样的事情,也许有帮助

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Process::Start("explorer.exe", "/open, C:\\Users\\mywindows\\source\\repos\\Kuliah\\Debug\\Praktikum Pertemuan 2.exe");
}

dont forget to add library不要忘记添加库

using namespace System::Diagnostics;

just try simple code试试简单的代码

Process::Start("explorer.exe");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM