SmartPascal
SelectDirectory
Function
Display a dialog to allow user selection of a directory FileCtrl unit
1  function SelectDirectory ( const Caption : string; const StartDir : WideString; out ChosenDir : string ) : Boolean;
2  function SelectDirectory ( var CurrDir : string; DialogOptions : TSelectDirOpts ; HelpContext : Longint ) : Boolean;
Description
The SelectDirectory displays a dialog allowing the user to select a directory ChosenDirectory (drive plus path).
 
Version 1.
 
This displays a Windows browser dialog, initialised to the specified StartDir. The Caption text is shown at the top of the dialog.
 
If the user presses OK, then the selected directory is returned in the ChosenDir variable, and the return value is True.
 
If the user presses Cancel, then no output is given, and the return value is False.
 
Version 2.
 
This displays a very different type of dialog, that incidently shows the files in the currently selected directory.
 
The CurrDir value is used to position the display to the given directory, and is updated with the selected value if the user presses OK.
 
The DialogOptions and HelpContext parameters are beyond the scope of this article. They may safely be left to default values, as in the given example.
Related commands
ChDir Change the working drive plus path for a specified drive
CreateDir Create a directory
GetCurrentDir Get the current directory (drive plus directory)
GetDir Get the default directory (drive plus path) for a specified drive
MkDir Make a directory
RemoveDir Remove a directory
RmDir Remove a directory
SetCurrentDir Change the current directory
ForceDirectories Create a new path of directories
 
Example code : Let the user select a dialog using the first version
var
  chosenDirectory : string;
begin
  // Ask the user to select a required directory, starting with C:
  if SelectDirectory('Select a directory', 'C:\', chosenDirectory)
  then ShowMessage('Chosen directory = '+chosenDirectory)
  else ShowMessage('Directory selection aborted');
end;
Show full unit code
   { Dialog displays - user selects C:\Program Files and hits OK }
  
   Chosen directory = C:\Program Files
 
Example code : Let the user select a dialog using the second version
var
  options : TSelectDirOpts;
  chosenDirectory : string;
begin
  chosenDirectory := 'C:\';  // Set the starting directory
  // Ask the user to select using a completely different dialog!
  if SelectDirectory(chosenDirectory, options, 0)
  then ShowMessage('Chosen directory = '+chosenDirectory)
  else ShowMessage('Directory selection aborted');
end;
Show full unit code
   { Dialog displays - user selects C:\Program Files and hits Cancel }
  
   Directory selection aborted