Implementing the “Reveal In Explorer” functionality

Update: I seems like Microsoft changed the behavior of the Explorer’s command line parameters in Vista and Windows 7. Below is the fixed version of my code that addresses the problem.

So, I was wondering the other day how to implement a functionality similar to Firefox’s “Open Containing Folder” or OS X’s “Reveal In Finder”.
It turned out to be extremely simple. Here is the Pascal/Delphi-Code:

uses
  ShellAPI;

function RevealInExplorer(const Filename: string; ShowExplorerWithFoldersBar: Boolean = True): Boolean;
var
  Params: string;
begin
  if FileExists(Filename) or DirectoryExists(Filename) then
  begin
    Params := Format('/select,"%s"', [Filename]);

    if ShowExplorerWithFoldersBar then
      Params := '/e,' + Params;

    ShellExecute(0, 'open', 'explorer.exe', PChar(Params), nil, SW_SHOWNORMAL);

    Result := True;
  end
  else
    Result := False;
end;

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.