You can also use the TBrowseForFolder action TBrowseForFolder ( stdActns.pas ):
var dir: string; begin with TBrowseForFolder.Create(nil) do try RootDir := 'C:\'; if Execute then dir := Folder; finally Free; end; end;
or use the WinApi function - SHBrowseForFolder directly (the second SelectDirectory overload uses it instead of the first overload, which creates its own delphi window with all the controls at runtime):
var dir : PChar; bfi : TBrowseInfo; pidl : PItemIDList; begin ZeroMemory(@bfi, sizeof(bfi)); pidl := SHBrowseForFolder(bfi); if pidl <> nil then try GetMem(dir, MAX_PATH + 1); try if SHGetPathFromIDList(pidl, dir) then begin // use dir end; finally FreeMem(dir); end; finally CoTaskMemFree(pidl); end; end;
source share