繁体   English   中英

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

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

当我使用目录路径启动资源管理器 window 时,会生成一个新的 explorer.exe 进程。 在我的程序退出后,explorer.exe 进程保持打开状态,这没关系,但是当我关闭该 explorer window 时,该进程不会关闭。 如果我在我的程序中生成了几个资源管理器 windows,我最终会得到许多 explorer.exe 进程,即使所有资源管理器 windows 都已关闭。

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

在此处输入图像描述

但是,如果我只启动没有第二个参数的 explorer.exe,即使打开了 explorer window,也不会创建额外的 explorer.exe 进程。 当 explore window 关闭时,除了桌面进程之一之外,您也看不到任何额外的 explorer.exe 进程。 请注意,我的桌面 explore.exe 是 PID 7704 是这个示例,应该是唯一一个正在运行的示例。

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

在此处输入图像描述

我发现如果我使用 cmd.exe 手动启动,这是真的。 在命令提示符下,如果您运行“explorer.exe C:/”,则可以创建相同的“悬空” explorer.exe 进程,而只运行“explorer.exe”则不会。

所以我的问题是如何在不创建所有这些额外的 explorer.exe 进程的情况下为我想要的文件夹打开资源管理器 window。 我还想知道“悬空”的 explorer.exe 进程是否最终会自行离开 go 或者他们会坐在那里并占用所有 memory 直到我不得不强制结束任务。 到目前为止,即使在 windows 关闭后不再有与它们相关的资源管理器 windows ,我也看到它们持续了好几天并且从未消失。

到目前为止,我唯一能想到的就是打开一个没有 arguments 的 explorer.exe,这样它就不会创建额外的进程,然后以某种方式找到打开的 explorer window 句柄然后用它浏览到我想要的目录. 我见过一些 DLLImport("user32.dll") 可能能够做到这一点。

这听起来比只使用 Process.Start 来立即访问我想要的文件夹要困难得多。 我只是不认为我的程序留下很多 memory 排放过程是一个好主意,即使它已经可以用 cmd.exe 来完成。 我不知道这是否是 Windows “功能”最终以 memory 的情况结束。

这是一个解决方案,它不创建额外的 explorer.exe 进程,并使用现有的进程。

从命令 shell 打开文件夹时,我意识到了这一点。 运行“explorer c:\temp”时,会创建一个新的 explorer.exe。 但是当运行"start c:\temp" ..

所以下面的代码启动一个隐藏的 cmd 进程,用"start <folder>\r\n"输入它的输入,然后用"exit\r\n"关闭它。 该代码还会检查新的 cmd 进程是否泄漏。

希望这可以帮助。

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

我刚刚碰巧做了这样的事情,也许有帮助

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

不要忘记添加库

using namespace System::Diagnostics;

试试简单的代码

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

暂无
暂无

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

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