Programmatically selecting file in explorer Programmatically selecting file in explorer windows windows

Programmatically selecting file in explorer


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);    }}


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.


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.