打开资源管理器窗口并等待其关闭

发布于 2024-11-10 00:30:16 字数 260 浏览 0 评论 0原文

我有一个程序正在打开某个文件夹的资源管理器窗口,但我想在资源管理器窗口关闭后立即执行操作,但如果我使用以下代码:

Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();

它正在根据需要打开资源管理器窗口,但 WaitForExit 命令具有没有效果,它就直接过去了。

是否有其他打开资源管理器窗口的方式可以让我知道用户何时关闭它?

I have a program that is opening an explorer window to a certain folder but i want to perform an action right after the explorer window is closed, but if I use the following code:

Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();

It is opening the explorer window as desired but the WaitForExit command has no effect and it just goes right past it.

Is there a different way of opening the explorer window that will be able to let me know when it is closed by the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-11-17 00:30:17

这个问题在 The Old New Thing:

WaitForSingleObject 立即返回的原因是 Explorer 是一个单实例程序(嗯,有限实例)。当您打开资源管理器窗口时,请求将被传递给正在运行的资源管理器副本,并且您启动的资源管理器副本将退出。这就是为什么你的 WaitForSingleObject 立即返回。

他提供了一些您可能会使用的解决方案(大量使用 PInvoke),例如使用 这个

最后,您可能会更轻松地使用其他类型的文件浏览器(可能来自您可以更好控制的 C# 库),而不是资源管理器。

The problem is explained pretty well at The Old New Thing:

The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your WaitForSingleObject returns immediately.

He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this.

In the end it might just be easier for you to use some other type of file browser maybe from a C# library somewhere that you have more control over, rather than explorer.

Spring初心 2024-11-17 00:30:17

无法重新生成错误。刚刚尝试过这个:

Process.Start("explorer.exe", "D:\\").WaitForExit();

它会阻止当前线程并等待我关闭资源管理器窗口。确保您没有在要阻止的线程之外的另一个线程上执行该命令。另请确保通过导入以下 .reg 文件将窗口的每个实例设置为启动 explorer.exe 的新实例:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"

您需要重新启动计算机才能生效。

Cannot regenerate the error. Just tried this:

Process.Start("explorer.exe", "D:\\").WaitForExit();

and it blocks the current thread and waits until I close the explorer windows. Make sure that you're not executing the command on another thread than the one you want to block. Also make sure that you set every instance of a window to start a new instance of explorer.exe via importing below .reg file:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"

You'll need to restart your computer for this to take effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文