Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Programmatically Select a File in Windows Explorer from Delphi code

Execute explorer.exe and Select a File

By Zarko Gajic, About.com

Programmatically Execute Explorer, Select a File

Programmatically Execute Explorer, Select a File

If you are working with files in your Delphi application, you might need to launch the Windows Explorer window and programmatically select a particular file in a given directory.

In order to select a file in Windows Explorer you first need to launch an instance of the "explorer.exe" using the ShellExecute API method.

Next, you need to know the exact Explorer switch (command line parameter) used to select a given file. To mark a file as selected, in Windows Explorer, the "/select,FILENAME" switch must be used.

Drop a TShellTreeView control (name: ShellTreeView1) on a Delphi form, and use the code below.

Note: You can also use the TFileListBox control or simply specify a file name in a string value.

uses ShellApi, ...
...
var
  selectedFileName : TFileName;
begin
  if ShellTreeView1.Selected = nil then exit;

  if NOT ShellTreeView1.SelectedFolder.IsFolder then
  begin
    selectedFileName := ShellTreeView1.SelectedFolder.PathName;

    ShellExecute(Handle,
                 'OPEN',
                 PChar('explorer.exe'),
                 PChar('/select, "' + selectedFileName + '"'),
                 nil,
                 SW_NORMAL) ;
  end;
end;
Delphi tips navigator:
» Displaying the Percentage Character in Delphi's Format function
« Register a File Command for a Windows Shell Menu from Delphi code
More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

More from About.com

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. Programmatically Select a File in Windows Explorer from Delphi code

©2008 About.com, a part of The New York Times Company.

All rights reserved.