Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically selecting file in explorer

Tags:

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.

like image 535
flashk Avatar asked Jun 09 '10 22:06

flashk


People also ask

How do I select Open with File Explorer?

If you'd rather have File Explorer open to This PC, go to the View tab and then select Options. In the Open File Explorer to: list, select This PC, and then select Apply.

How can we select the folder with Explorer explain?

Press Windows key + E to open File Explorer. Press Tab until you're in the section of the window containing the files or folders you want to select. Use the arrow keys to move to the file or folder you want to select.

How do I explore a file?

To open File Explorer, click on the File Explorer icon located in the taskbar. Alternatively, you can open File Explorer by clicking on the Start button and then clicking on File Explorer.

How do I filter search in File Explorer?

Using Search Filters. If you do a single Click on the search box in the top-right of your explorer window, it will pop-up with a small list of recent searches, and available search filters below them. From here you can Click a Search filter to add exactly what you want to find.


2 Answers

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);
    }
}
like image 199
flashk Avatar answered Sep 23 '22 02:09

flashk


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.

like image 34
user732592 Avatar answered Sep 24 '22 02:09

user732592