Back


Lennie's Weekly Tip: The SelectDirectory function - Part I
20 August 2000 Ref. 34

For Delphi 3.0 and Later

You can now use the SelectDirectory function to bring up a dialog to allow the user to enter a directory name or choose it from the Windows directory browser.

The SelectDirectory function has two parts, the first syntax (discussed in this tip) will call the Windows directory browser, where the second (discussed in Part II) will call the select directory dialog box.

Declaration
-----------

function SelectDirectory(const Caption: string; const Root: WideString; out Directory: string): Boolean; overload;

Description
-----------
Use the first syntax to display the Windows directory browser. The Caption parameter specifies a caption for the dialog.
The Root parameter specifies the root directory from which to browse.
The selected directory is returned as the Directory parameter.

SelectDirectory returns TRUE if the user selected a directory and chose OK, and FALSE if the user chose Cancel or closed the dialog box without selecting a directory.

Example
-------

uses
  FileCtrl;

var
  Dir: string;

begin
  if (SelectDirectory('Working Directory', '', Dir)) then
    Label1.Caption := 'Working Directory:  ' + Dir
  else
    Label1.Caption := 'No working directory selected';
end;

In this example the SelectDirectory function will display the Windows directory browser. If the user selected a directory and choose OK, the full-path will be return inside the "Dir" variable and displayed in the Label, otherwise the Label will respond that no directory have been selected.
 

SelectDirectory will return the selected directory as a long file path...if you need to translate it into a short path, then take a look at my tip "Convert Long to Short Paths" Ref. 32
 

NOTE: Remember to add the FileCtrl unit to your application's uses-clause.


Back