Search code examples
delphidirectorydelphi-2010opendialog

DELPHI - How to use opendialog1 for choosing a folder?


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


Solution

  • 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;