用于打开Windows资源管理器(或焦点,如果存在)并选择文件的代码

时间:2023-01-17 08:35:41

My goal is to write a C# code that will open a Windows Explorer window, with a particular file selected. If such window is already open, I want to bring it to front. I have tried two options.

我的目标是编写一个C#代码,它将打开一个Windows资源管理器窗口,并选择一个特定的文件。如果这样的窗口已经打开,我想把它带到前面。我尝试了两种选择。

First, I start by explicitly calling explorer.exe:

首先,我首先明确调用explorer.exe:

arg = "/select, " + pathToFile;
Process.Start("explorer.exe", arg);

This opens and selects a window fine, but the problem is that it will always open a new window, even if one exists. So I tried this:

这将打开并选择一个正常的窗口,但问题是它将始终打开一个新窗口,即使存在一个窗口。所以我尝试了这个:

Process.Start(pathToDir);

This either opens a new window or focuses an old one, but gives me no option to select a file.

这会打开一个新窗口或聚焦旧窗口,但不能选择文件。

What can I do? I looked at explorer's arguments and I don't see anything I can use. A last-resort option I can come up with is to get the list of already open windows and use some WINAPI-level code to handle it, but that seems like an overkill.

我能做什么?我查看了探险家的论点,我没有看到任何可以使用的东西。我能想到的最后一个选择是获取已经打开的窗口列表并使用一些WINAPI级别的代码来处理它,但这似乎是一种矫枉过正。

1 个解决方案

#1


12  

I don't know if it's possible using process start, but the following code opens the Windows explorer on the containing folder only if needed (if the folder is already open, or selected on another file, it's reused) and selects the desired file.

我不知道是否可以使用进程启动,但以下代码仅在需要时打开包含文件夹的Windows资源管理器(如果文件夹已打开,或在另一个文件上选中,则重复使用)并选择所需的文件。

It's using p/invoke interop code on the SHOpenFolderAndSelectItems function:

它在SHOpenFolderAndSelectItems函数上使用p / invoke互操作代码:

public static void OpenFolderAndSelectFile(string filePath)
{
    if (filePath == null)
        throw new ArgumentNullException("filePath");

    IntPtr pidl = ILCreateFromPathW(filePath);
    SHOpenFolderAndSelectItems(pidl, 0, IntPtr.Zero, 0);
    ILFree(pidl);
}

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr ILCreateFromPathW(string pszPath);

[DllImport("shell32.dll")]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);

[DllImport("shell32.dll")]
private static extern void ILFree(IntPtr pidl);

#1


12  

I don't know if it's possible using process start, but the following code opens the Windows explorer on the containing folder only if needed (if the folder is already open, or selected on another file, it's reused) and selects the desired file.

我不知道是否可以使用进程启动,但以下代码仅在需要时打开包含文件夹的Windows资源管理器(如果文件夹已打开,或在另一个文件上选中,则重复使用)并选择所需的文件。

It's using p/invoke interop code on the SHOpenFolderAndSelectItems function:

它在SHOpenFolderAndSelectItems函数上使用p / invoke互操作代码:

public static void OpenFolderAndSelectFile(string filePath)
{
    if (filePath == null)
        throw new ArgumentNullException("filePath");

    IntPtr pidl = ILCreateFromPathW(filePath);
    SHOpenFolderAndSelectItems(pidl, 0, IntPtr.Zero, 0);
    ILFree(pidl);
}

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr ILCreateFromPathW(string pszPath);

[DllImport("shell32.dll")]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);

[DllImport("shell32.dll")]
private static extern void ILFree(IntPtr pidl);