Home > Windows, Windows API > Programmatically selecting a file in windows explorer!

Programmatically selecting a file in windows explorer!

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

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

See command line arguments for explorer.exe on XP

  1. November 26, 2008 at 8:33 am


    Did you try with a comma, what happened?

  2. November 26, 2008 at 12:56 am

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


  3. November 1, 2008 at 12:42 pm

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

  4. Artur Perwenis
    November 1, 2008 at 12:52 am

    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. October 22, 2008 at 1:24 pm

    Ah ok, Sorry I misunderstood. 🙂

  6. jongampark
    October 13, 2008 at 12:32 am

    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. September 4, 2008 at 3:39 am

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

  8. jongampark
    September 3, 2008 at 6:35 pm

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

  9. December 20, 2007 at 3:36 am

    🙂 Wow great!

  10. December 19, 2007 at 4:33 pm

    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

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: