Select a folder with a native VCL file dialog

Use TFileOpenDialog with fdoPickFolders when a VCL application needs a directory. This uses the native Windows file dialog in folder-selection mode. It avoids disguising a file picker with a fake filename or relying on undocumented control behavior.

The following helper targets Delphi 13 VCL on current Windows. It returns a Boolean for confirmation and a separate path. Cancellation is a normal result and should preserve the application's existing selection.

Add a focused helper unit

Create a VCL Forms Application and add a unit named FolderPicker. Replace that unit with:

Pascal
unit FolderPicker;

interface

uses
  Winapi.Windows;

function TryChooseFolder(OwnerWindow: HWND; const InitialFolder: string; out SelectedFolder: string): Boolean;

implementation

uses
  System.SysUtils,
  Vcl.Dialogs;

function TryChooseFolder(OwnerWindow: HWND; const InitialFolder: string; out SelectedFolder: string): Boolean;
var
  Dialog: TFileOpenDialog;
begin
  SelectedFolder := '';
  Dialog := TFileOpenDialog.Create(nil);
  try
    Dialog.Title := 'Choose an export folder';
    Dialog.Options := [fdoPickFolders, fdoForceFileSystem, fdoPathMustExist];
    if DirectoryExists(InitialFolder) then
      Dialog.DefaultFolder := InitialFolder;
    Result := Dialog.Execute(OwnerWindow);
    if Result then
      SelectedFolder := Dialog.FileName;
  finally
    Dialog.Free;
  end;
end;

end.

fdoPickFolders changes the selection target. fdoForceFileSystem requires a filesystem item instead of an arbitrary shell namespace item. fdoPathMustExist asks the dialog to select an existing path. The helper owns the dialog because it creates it without a component owner, and its finally block releases it on confirmation, cancellation or an exception.

Apply a selection only after confirmation

Add FolderPicker to the form unit's uses clause. Add a button named ChooseButton, a label named FolderLabel, and a private string field named FExportFolder. Connect the button's click event to:

Pascal
procedure TForm1.ChooseButtonClick(Sender: TObject);
var
  Selected: string;
begin
  if TryChooseFolder(Handle, FExportFolder, Selected) then
  begin
    FExportFolder := Selected;
    FolderLabel.Caption := FExportFolder;
  end;
end;

Selecting a folder and confirming should update the stored path and label. Clicking Cancel should leave both unchanged. The helper's output string is empty on cancellation, but the caller does not assign that value to its current state.

Passing the form's handle establishes the intended owner window for the modal dialog. Call the helper on the VCL main thread in the normal initialized application. It is not a worker-thread file-system service, and opening it from a background task would require a different UI coordination design.

Validate the actual operation

An existing selected directory is not a promise that a later write will succeed. Permissions, network availability, removable media and concurrent changes can invalidate the path after the dialog closes. Open or create the required file at the point of use and handle that operation's error.

Avoid creating a probe file merely to make the dialog return success. The picker selects a location. The later export operation owns its own creation and cleanup policy. If an export can replace an existing file, present that decision in the export workflow rather than hiding it inside directory selection.

Store the selected path as text without assuming it ends in a separator. Use path-combining APIs when constructing child paths. Test spaces, non-ASCII names, network shares relevant to the application and cancellation after a previous successful selection.

This helper uses the current native dialog. A product supporting older unsupported Windows versions would need a separately defined fallback, but a current VCL application should not add one merely to reproduce an old TOpenDialog workaround. Final acceptance requires opening the dialog and checking ownership, keyboard navigation and cancellation in the real UI.

References

TFileOpenDialog

TFileDialogOption

TCustomFileDialog.Execute

Question provenance

This article addresses Selecting a directory with TOpenDialog , asked by ple103 on Stack Overflow. The source question is licensed under CC BY-SA 3.0 . The explanation and examples here are newly authored for current Delphi. No answer text or code is reproduced.

Author: Herman Schoenfeld