简体   繁体   English

打开资源管理器 window 并等待它关闭

[英]Open explorer window and wait for it to close

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:我有一个程序正在打开资源管理器 window 到某个文件夹,但我想在资源管理器 window 关闭后立即执行操作,但如果我使用以下代码:

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.它正在根据需要打开资源管理器 window 但 WaitForExit 命令没有效果,它只是通过它。

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?是否有其他方法可以打开资源管理器 window,当它被用户关闭时可以让我知道?

The problem is explained pretty well at The Old New Thing : The Old New Thing很好地解释了这个问题:

The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). WaitForSingleObject 立即返回的原因是 Explorer 是一个单实例程序(嗯,有限实例)。 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.当您打开 Explorer window 时,请求将移交给正在运行的 Explorer 副本,并且您启动的 Explorer 副本退出。 That's why your WaitForSingleObject returns immediately.这就是您的 WaitForSingleObject 立即返回的原因。

He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this .他提供了一些您可能会使用的解决方案(大量使用 PInvoke),例如使用类似这样的东西。

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.最后,您可能更容易使用其他类型的文件浏览器,可能来自 C# 库,您可以控制更多的地方,而不是资源管理器。

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.它会阻塞当前线程并等待直到我关闭资源管理器 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:还要确保通过导入 below.reg 文件将 window 的每个实例设置为启动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"

You'll need to restart your computer for this to take effect.您需要重新启动计算机才能使其生效。

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

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