Selecting a directory with TOpenDialog
You can use the TFileOpenDialog
(on Vista+):
with TFileOpenDialog.Create(nil) do
try
Options := [fdoPickFolders];
if Execute then
ShowMessage(FileName);
finally
Free;
end;
Personally, I always use the TFileOpenDialog
on Vista+ and fallback using the SelectDirectory
(the good one!) on XP, like this:
if Win32MajorVersion >= 6 then
with TFileOpenDialog.Create(nil) do
try
Title := 'Select Directory';
Options := [fdoPickFolders, fdoPathMustExist, fdoForceFileSystem]; // YMMV
OkButtonLabel := 'Select';
DefaultFolder := FDir;
FileName := FDir;
if Execute then
ShowMessage(FileName);
finally
Free;
end
else
if SelectDirectory('Select Directory', ExtractFileDrive(FDir), FDir,
[sdNewUI, sdNewFolder]) then
ShowMessage(FDir)
You do know that the two overloaded functions called FileCtrl.SelectDirectory
produce entirely different dialogs, right?
SelectDirectory(s, [], 0);
SelectDirectory('Select a directory', s, s, []);
Just include
FileCtrl.pas
var
sDir:String;
begin
SelectDirectory('Your caption','',sDir);
end;
Just leave second argument empty if want to see all directories including desktop. If you set second argument to any valid Path, then your dialog will have that path to top folder and you can not navigate beyond that.
For example:
SelectDirectory('Your caption','C:\',sDir)
will not let you select anything beyond C:\
, like D:\
or E:\
etc.
So it is good to leave it empty.