Functions Delphi

function PromptForFileName ( var FileName : string; { const Filter : string; const DefaultExt : string; const Title : string; const InitialDir : string; SaveDialog : Boolean = False } ) : Boolean;


Description
The PromptForFileName function presents a dialog to the user allowing navigation to, and selection of a file.

If the user presses OK the FileName variable is updated with the full drive/path/filename value for the selected file, and the return value is True.

If the user presses Cancel, no updates are done, and the return value is False.

The remaining parameters are all optional (although parameters used on the right mandate use of the preceding parameters) :

Filter

Used to limit the types of file displayed. The format is as follows :

'Description|filter{|...}'

For example, to show only .txt and .pas files :

Text files (*.txt)|*.txt|Delphi files (*.pas)|*.pas

DefaultExt

Used to define an extension to add to a new file name (if SaveDialog is True).

Title

Used to give a title to the dialog.

InitialDir

Positions the dialog at a given directory. For example :

'C:Program Files'

SaveDialog

Determines whether a new file can be selected for saving.

Related commands
InputBox Display a dialog that asks for user text input, with default
InputQuery Display a dialog that asks for user text input
ShowMessage Display a string in a simple dialog with an OK button
ShowMessageFmt Display formatted data in a simple dialog with an OK button
ShowMessagePos Display a string in a simple dialog at a given screen position
TOpenDialog Displays a file selection dialog
TSaveDialog Displays a dialog for selecting a save file name

Example code : Use this dialog to select a text file
var
selectedFile : string;
begin
// Ask the user to select a file
if PromptForFileName(selectedFile,
'Text files (*.txt)|*.txt',
'',
'Select your project file',
'C:\',
False) // Means not a Save dialog
then
// Display this full file/path value
ShowMessage('Selected file = '+selectedFile)
else
ShowMessage('Cancel pressed');
end;

Show full unit code
{ In the dialog, user selects C:\Files\data.txt and hits OK }

Selected file = C:\Files\data.txt