Possible Duplicate:
Delphi: Selecting a directory with TOpenDialog
I need to open a specific folder on my project. When I use opendialog1, I can only open a file. How about opening a folder ?
PS : I use Delphi 2010
Possible Duplicate:
Delphi: Selecting a directory with TOpenDialog
I need to open a specific folder on my project. When I use opendialog1, I can only open a file. How about opening a folder ?
PS : I use Delphi 2010
On Vista and up you can show a more modern looking dialog using TFileOpenDialog
.
var
OpenDialog: TFileOpenDialog;
SelectedFolder: string;
.....
OpenDialog := TFileOpenDialog.Create(MainForm);
try
OpenDialog.Options := OpenDialog.Options + [fdoPickFolders];
if not OpenDialog.Execute then
Abort;
SelectedFolder := OpenDialog.FileName;
finally
OpenDialog.Free;
end;
which looks like this:
You're looking for SelectDirectory
in the FileCtrl
unit. It has two overloaded versions:
function SelectDirectory(var Directory: string;
Options: TSelectDirOpts; HelpCtx: Longint): Boolean;
function SelectDirectory(const Caption: string; const Root: WideString;
var Directory: string; Options: TSelectDirExtOpts; Parent: TWinControl): Boolean;
The one you want to use depends on the version of Delphi you're using, and the specific appearance and functionality you're looking for; I( usually find the second version works perfectly for modern versions of Delphi and Windows, and users seem happy with the "normally expected appearance and functionality".
You also can use TBrowseForFolder
action class (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 WinApi function - SHBrowseForFolder
directly (second SelectDirectory
overload uses it, instead of first overload, which creates own delphi-window with all 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;