Selecting a directory with TOpenDialog

后端 未结 5 1037
长情又很酷
长情又很酷 2020-12-04 17:40

I\'d really like to know the various ways I could select a directory with the TOpenDialog, whether it be downloading a new component or using what is provided by Delphi, but

5条回答
  •  星月不相逢
    2020-12-04 18:20

    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)
    

提交回复
热议问题