以编程方式在资源管理器中选择文件 [英] Programmatically selecting file in explorer

查看:381
本文介绍了以编程方式在资源管理器中选择文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我可以通过编程方式打开浏览器并使用以下代码选择一个文件:

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.

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

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?

编辑

看来Vista / Win7上的行为是不同的。

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.

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

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.

如果有人知道Visual Studio

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

推荐答案

找到我的问题的答案。我需要使用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:

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

这篇关于以编程方式在资源管理器中选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆