Programmatically selecting file in explorer

Posted by flashk on Stack Overflow See other posts from Stack Overflow or by flashk
Published on 2010-06-09T22:00:17Z Indexed on 2010/06/09 22:02 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about Windows