bits and bytes

Jesus Loves You. C, C++, VC++, .net code snippets and articles, based on Nibu’s day to day programming experiences.

Programmatically selecting a file in windows explorer!

Posted by Nibu Thomas on December 19, 2007

Well while coding for process viewer I felt the need for programmatically selecting a file in windows explorer and after a short search bumped into about.com which had a code snippet in Delphi which does this.

Modified a “bit” for VC++…

// Select a file in explorer
 void SelectFileInExplorer( LPCTSTR lpctszFileToSelect_i )
 {
    // This is the command line for explorer which tells it to select the given file
    CString csCommandLine = _T( "/Select," );
    csCommandLine +=  lpctszFileToSelect_i;

    // Prepare shell execution params
    SHELLEXECUTEINFO shExecInfo   = { 0 };
    shExecInfo.cbSize             = sizeof( shExecInfo );
    shExecInfo.lpFile             = _T( "Explorer.exe" );
    shExecInfo.lpParameters       = csCommandLine;
    shExecInfo.nShow              = SW_SHOWNORMAL;
    shExecInfo.lpVerb             = _T( "Open" ); // Context menu item    

    // Just have a look in MSDN to see the relevance of these flags
    shExecInfo.fMask              = SEE_MASK_INVOKEIDLIST | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;    

    // Select file in explorer
    VERIFY( ShellExecuteEx( &shExecInfo ));
 }// End SelectFileInExplorer

See command line arguments for explorer.exe on XP

10 Responses to “Programmatically selecting a file in windows explorer!”

  1. Hi,

    Did you try with a comma, what happened?

  2. bibibubu said

    But what if there are comma sign “,” in file name?

    bibibubu

  3. Thanks mate! Useful for people trying to do this in C#.

  4. Artur Perwenis said

    And in C# the shorter version is:
    private void Locate_Click(string fileName)
    {
    string cmdLine = string.Format(CultureInfo.InvariantCulture, “/Select,{0}”, fileName);
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = “explorer.exe”;
    psi.Arguments = cmdLine;
    var proc = Process.Start(psi);
    }

  5. Ah ok, Sorry I misunderstood. :)

  6. jongampark said

    No.. what I mean is..
    For example, when you open a file in a Visual Studio, you click its file name from its title or in the Solution Explorer, and do “Open its containing folder” or “Locate it in the Explorer” and so on.

    Currently, in tabbed window scheme of the VS, it provides “Open its containing folder”, but in other scheme, it doesn’t provide such a feature. Also, the Solution Explorer doesn’t have the feature.

    In the “Multiple Windows” layout, it is pretty hard to confirm the opened file’s location. Using property pane is sometimes impossible.

  7. I guess its already there in the Find files application. Isn’t it? :?

  8. jongampark said

    :) I wish the MS would add this to the OS-wise service.

  9. nibuthomas said

    :) Wow great!

  10. It must be a coincidence but that was what i was looking for right now :) )) and i was browsing CodeProject and saw your blog address :) )

    Thnx man

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>