5

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 ?

wanted - open folder dialog in Delphi

PS : I use Delphi 2010

Community
  • 1
  • 1
Galvion
  • 1,353
  • 7
  • 23
  • 35
  • 4
    Ken's answer (the only one so far) is great, but this seems like a dup of: http://stackoverflow.com/questions/7422689/delphi-selecting-a-directory-with-topendialog – Argalatyr Mar 02 '12 at 06:19
  • In the fact, you can use the `TOpenDialog` descendant - `TSaveDialog` (yeah, thats quite quick and dirty) – OnTheFly Mar 02 '12 at 07:14
  • 2
    Voted to close, but I'll be missing [`teran's answer`](http://stackoverflow.com/a/9529154/960757) there. – TLama Mar 02 '12 at 10:47

3 Answers3

19

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:

enter image description here

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
11

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".

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 2
    +1 Ken, btw the unit name is `FileCtrl`. – RRUZ Mar 02 '12 at 05:08
  • 1
    Both functions were moved out of the `FileCtrl` unit a long time ago. – Remy Lebeau Mar 02 '12 at 06:40
  • Rodrigo, thanks for the correction. Fixed. @Remy, the [docs for XE2](http://docwiki.embarcadero.com/VCL/en/FileCtrl.SelectDirectory) say you're wrong. If they were moved "a long time ago" the docs should mention that fact. – Ken White Mar 02 '12 at 19:23
  • Nevermind. Other `FileCtrl` functions were indeed moved to `SysUtils` a long time ago and their `FileCtrl` references were deprecated. I thought `SelectDirectory()` was amongst them, but I just checked and that is not the case afterall. – Remy Lebeau Mar 03 '12 at 00:23
6

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
teran
  • 3,214
  • 1
  • 21
  • 30