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

发布于 2024-09-05 00:09:48 字数 914 浏览 4 评论 0原文

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

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

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

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

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

有没有办法强制资源管理器更新选择或更好的方法来完成此任务?

编辑:

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

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

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

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.txt will 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.

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.

My main goal is to replicate the Visual Studio option to Open Containing Folder of 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.

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

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

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

发布评论

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

评论(3

等风来 2024-09-12 00:09:48

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

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

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:

void BrowseToFile(LPCTSTR filename)
{
    ITEMIDLIST *pidl = ILCreateFromPath(filename);
    if(pidl) {
        SHOpenFolderAndSelectItems(pidl,0,0,0);
        ILFree(pidl);
    }
}
月下凄凉 2024-09-12 00:09:48

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

/n,/select,<path_and_filename>

SHOpenFolderAndSelectItems 在我的情况下总是失败,我不明白为什么。顺便说一句,在调用此函数之前,您必须先调用 CoInitialize/CoInitializeEx。

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

/n,/select,<path_and_filename>

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

夜雨飘雪 2024-09-12 00:09:48

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

虽然这感觉像是一个拼凑,但您可以检测 XP,并且仅对于该操作系统,使用其句柄关闭对话框,然后打开一个新对话框来定位另一个文件。

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.

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