在资源管理器中以编程方式选择文件
在我的应用程序中,我可以以编程方式打开资源管理器并使用以下代码选择一个文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了我的问题的答案。我需要使用 shell 函数
SHOpenFolderAndSelectItems
。如果有人感兴趣的话,这里是该函数的代码: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:尝试“/n”选项。然而,这将打开一个新文件夹 - 可能已经打开。但是,至少您指定的文件被选中。
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.
SHOpenFolderAndSelectItems always fails in my case and I can't figure out why. Btw, you must call CoInitialize/CoInitializeEx before calling this one.
在您概述的情况下,文件窗口仅在初始化时而不是激活时选择文件。
虽然这感觉像是一个拼凑,但您可以检测 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.