如果 Windows 资源管理器在特定路径打开,则不要创建新实例 [英] If Windows explorer is open at a specific path, do not create a new instance
问题描述
我正在使用以下代码,以便当用户单击按钮时,会在特定路径上打开 Windows 资源管理器 的一个实例.但这会导致 Explorer 的一个新实例被打开.
I am using the following code so that when the user clicks on a button, an instance of Windows Explorer is opened at a specific path. But this causes a new instance of the Explorer to be opened.
我想改变它,如果 Explorer 已经在同一路径中打开,程序不会创建新进程,而是将打开的实例放在前面.
I want to change it so that, if Explorer is already open in the same path, the program does not create a new process and instead bring the open instance to front.
private void button_Click(object sender, EventArgs e)
{
if (Directory.Exists(myPath))
Process filesFolder = Process.Start("explorer.exe", Conf.FilesLocation);
}
推荐答案
您可以使用打开"动词,这将在资源管理器中打开目录并重新使用现有的 explorer.exe,如果您将其传递给它的目录已经开放:所以,假设 Conf.FilesLocation
是一个目录:
You can use the "open" verb, which will open directories in explorer and re-use an an existing explorer.exe if you pass it a directory that it already has open:
So, assuming Conf.FilesLocation
is a directory:
var proc = new ProcessStartInfo();
proc.FileName = Conf.FilesLocation;
proc.Verb = "open";
proc.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(proc );
这篇关于如果 Windows 资源管理器在特定路径打开,则不要创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!