Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Programmatically Select a File in Windows Explorer from Delphi code

Title: Programmatically Select a File in Windows Explorer from Delphi code 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;