windows 在资源管理器中以编程方式选择文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3010305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:37:35  来源:igfitidea点击:

Programmatically selecting file in explorer

c++windowsexplorershellexecute

提问by flashk

In my application I can programmatically open explorer and select a file using the following code:

在我的应用程序中,我可以使用以下代码以编程方式打开资源管理器并选择一个文件:

void BrowseToFile(LPCTSTR filename)
{
    CString strArgs; 
    strArgs = _T("/select,\"");
    strArgs += filename; 
    strArgs += _T("\"");

    ShellExecute(0, _T("open"), _T("explorer.exe"), strArgs, 0, SW_NORMAL);
}

My problem is that if I call this function a second time with a different file, but in the same folder, the selection in explorer does not change to the new file, but remains on the previous file.

我的问题是,如果我使用不同的文件再次调用此函数,但在同一文件夹中,资源管理器中的选择不会更改为新文件,而是保留在前一个文件中。

For example, if I call my function with C:\path\to\file1.txt, a new explorer window will open and file1.txtwill be selected. If I call my function a second time with C:\path\to\file2.txt, the existing explorer window will be activated, but the selection will still be on file1.txt.

例如,如果我用 调用我的函数C:\path\to\file1.txt,一个新的资源管理器窗口将打开并被file1.txt选中。如果我第二次使用 调用我的函数C:\path\to\file2.txt,现有的资源管理器窗口将被激活,但选择仍将打开file1.txt

Is there a way to force explorer to update the selection or a better way to accomplish this?

有没有办法强制资源管理器更新选择或更好的方法来实现这一点?

EDIT:

编辑:

The behavior mentioned above was on Windows XP. It seems the behavior on Vista / Win7 is different. Each call will open a new instance of explorer and select the file.

上面提到的行为是在 Windows XP 上进行的。Vista / Win7 上的行为似乎有所不同。每次调用都会打开一个新的资源管理器实例并选择文件。

My main goal is to replicate the Visual Studio option to Open Containing Folderof a document. This feature in Visual Studio behaves the same on XP, Vista, and Win7. It will not create a new instance if another instance with the same folder is already open, but it will update the selection to the new file.

我的主要目标是复制 Visual Studio 选项以打开文档的包含文件夹。Visual Studio 中的此功能在 XP、Vista 和 Win7 上的行为相同。如果具有相同文件夹的另一个实例已打开,则不会创建新实例,但会将选择更新为新文件。

If anybody knows how Visual Studio accomplishes this I would love to know about it.

如果有人知道 Visual Studio 如何实现这一点,我很想知道。

回答by flashk

Found the answer to my question. I need to use the shell function SHOpenFolderAndSelectItems. Here is the code for the function if anybody is ever interested:

找到了我的问题的答案。我需要使用shell函数SHOpenFolderAndSelectItems。如果有人感兴趣,这里是该函数的代码:

void BrowseToFile(LPCTSTR filename)
{
    ITEMIDLIST *pidl = ILCreateFromPath(filename);
    if(pidl) {
        SHOpenFolderAndSelectItems(pidl,0,0,0);
        ILFree(pidl);
    }
}

回答by user732592

Try the '/n' option. This will, however, open a new folder - perhaps already opened. But, at least, the file you specify is selected.

试试“/n”选项。但是,这将打开一个新文件夹 - 可能已经打开。但是,至少选择了您指定的文件。

/n,/select,<path_and_filename>

SHOpenFolderAndSelectItemsalways fails in my case and I can't figure out why. Btw, you must call CoInitialize/CoInitializeEx before calling this one.

SHOpenFolderAndSelectItems在我的情况下总是失败,我不知道为什么。顺便说一句,在调用这个之前你必须先调用 CoInitialize/CoInitializeEx。

回答by John K

In the case you outlined it appears the file window only selects the file when it's initialized instead of when activated.

在您概述的情况下,文件窗口仅在初始化时而不是在激活时选择文件。

Although this feels like a kludge, you could detect XP and only for that OS close the dialog using its handle and open a new one to target another file with.

尽管这感觉像是一团糟,但您可以检测到 XP,并且仅针对该操作系统使用其句柄关闭对话框并打开一个新对话框以定位另一个文件。