How to open Explorer with a specific file selected?

前端 未结 4 1112
误落风尘
误落风尘 2020-12-08 00:01

I would like to code a function to which you can pass a file path, for example:

C:\\FOLDER\\SUBFOLDER\\FILE.TXT

and it would open Windows E

4条回答
  •  情话喂你
    2020-12-08 00:46

    The supported method since Windows XP (i.e. not supported on Windows 2000 or earlier) is SHOpenFolderAndSelectItems:

    void OpenFolderAndSelectItem(String filename)
    {
       // Parse the full filename into a pidl
       PIDLIST_ABSOLUTE pidl;
       SFGAO flags;
       SHParseDisplayName(filename, null, out pidl, 0, out flags);
       try 
       {
          // Open Explorer and select the thing
          SHOpenFolderAndSelectItems(pidl, 0, null, 0);
       }
       finally 
       {
          // Use the task allocator to free to returned pidl
          CoTaskMemFree(pidl);
       }
    }
    

提交回复
热议问题