How to open Explorer with a specific file selected?

前端 未结 4 1100
误落风尘
误落风尘 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:43

    To follow up on @Mahmoud Al-Qudsi's answer. when he says "launching the process", this is what worked for me:

    // assume variable "path" has the full path to the file, but possibly with / delimiters
    for ( int i = 0 ; path[ i ] != 0 ; i++ )
    {
        if ( path[ i ] == '/' )
        {
            path[ i ] = '\\';
        }
    }
    std::string s = "explorer.exe /select,\"";
    s += path;
    s += "\"";
    PROCESS_INFORMATION processInformation;
    STARTUPINFOA startupInfo;
    ZeroMemory( &startupInfo, sizeof(startupInfo) );
    startupInfo.cb = sizeof( STARTUPINFOA );
    ZeroMemory( &processInformation, sizeof( processInformation ) );
    CreateProcessA( NULL, (LPSTR)s.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInformation );
    
    0 讨论(0)
  • 2020-12-08 00:44

    When executing the command if your path contains multiple slashes then it will not open the folder and select the file properly Please make sure that your file path should be like this

    C:\a\b\x.txt

    instead of

    C:\\a\\b\\x.txt

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

    Easiest way without using Win32 shell functions is to simply launch explorer.exe with the /select parameter. For example, launching the process

    explorer.exe /select,"C:\Folder\subfolder\file.txt"

    will open a new explorer window to C:\Folder\subfolder with file.txt selected.

    If you wish to do it programmatically without launching a new process, you'll need to use the shell function SHOpenFolderAndSelectItems, which is what the /select command to explorer.exe will use internally. Note that this requires the use of PIDLs, and can be a real PITA if you are not familiar with how the shell APIs work.

    Here's a complete, programmatic implementation of the /select approach, with path cleanup thanks to suggestions from @Bhushan and @tehDorf:

    public bool ExploreFile(string filePath) {
        if (!System.IO.File.Exists(filePath)) {
            return false;
        }
        //Clean up file path so it can be navigated OK
        filePath = System.IO.Path.GetFullPath(filePath);
        System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
        return true;
    }
    

    Reference: Explorer.exe Command-line switches

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