1. Technology

Programmatically Select a File in Windows Explorer from Delphi code

Execute explorer.exe and Select a File


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, ...
   selectedFileName : TFileName;
   if ShellTreeView1.Selected = nil then exit;
   if NOT ShellTreeView1.SelectedFolder.IsFolder then
     selectedFileName := ShellTreeView1.SelectedFolder.PathName;
                  PChar('/select, "' + selectedFileName + '"'),
                  SW_NORMAL) ;
Delphi tips navigator:
» Displaying the Percentage Character in Delphi's Format function
« Register a File Command for a Windows Shell Menu from Delphi code
Related Video
Add Sound to PowerPoint

©2014 About.com. All rights reserved.