Programmatically selecting file in explorer

后端 未结 3 1493
天涯浪人
天涯浪人 2020-12-05 03:12

In my application I can programmatically open explorer and select a file using the following code:

void BrowseToFile(LPCTSTR filename)
{
    CString strArgs;         


        
相关标签:
3条回答
  • 2020-12-05 03:37

    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.

    0 讨论(0)
  • 2020-12-05 03:44

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 03:50

    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.

    0 讨论(0)
提交回复
热议问题