解決方法
改爲使用TFileOpenDialog
*。
設置FileOpenDialog1.Options:= [fdoPickFolders,fdoPathMustExist]

現在你有一個對話框:
*)不與TOpenDialog,不允許您只選擇文件夾混淆。
爲Windows XP
注意解決方案,新的TFileOpenDialog僅適用於Vista和上面。
如果包含此控件,則您的程序將無法在XP上運行。
如果您在XP上啓動對話框,它將生成一個EPlatformVersionException
。
您可能需要使用下面的代碼代替,如果你想成爲向後兼容:
uses JclSysInfo; //because you have XE use JCL.
...
var
WinMajorVer: Integer;
Directory: string;
FileDialog: TFileOpenDialog;
begin
WinMajorVer:= GetWindowsMajorVersionNumber;
if WinMajorVer < 6 then begin //pre-vista
//To show the root Desktop namespace, you should be setting the Root parameter to an empty string ('') instead of 'Desktop'
FileCtrl.SelectDirectory('Caption', '', Directory, [sdNewUI, sdShowEdit]);
end else begin
FileDialog:= TFileOpenDialog.Create(self);
try
FileDialog.Options:= [fdoPickFolders,fdoPathMustExist];
if FileDialog.Execute then Directory:= FileOpenDialog1.FileName;
finally
FileDialog.Free;
end;
end;
Result:= Directory;
end;
推薦閱讀:
detect windows version
編輯
FileCtrl.SelectDirectory('Caption', 'Desktop', Directory, [sdNewUI, sdShowEdit]);
的'Desktop'
進入Root
參數,它是像這樣處理的:
...
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil,
Root, Eaten, RootItemIDList, Flags);
...
下面是MSDN對IDesktopFolder.ParseDisplayName有說:
pszDisplayName [中]
類型:LPWSTR
以空結尾的Unicode字符串與顯示名稱。由於每個Shell文件夾都定義了自己的解析語法,因此該字符串可以採用的形式可能會有所不同例如,桌面文件夾接受諸如「C:\ My Docs \ My File.txt」之類的路徑。它還將使用「:: {GUID}」語法接受對名稱空間中具有與它們關聯的GUID的項目的引用。
請注意,文檔指出桌面文件夾將接受路徑和GUID。它不接受'Desktop'
。因爲那既不是。
'Desktop'
作爲root
作用於一個系統而不是另一個系統的事實是在IDesktopFolder
接口的較舊/較新版本中進行的一些未公開的修復。
技術方案
使用''
如所示在我的代碼上方的「根」。
顯然SelectDirectory
是微軟公司的一個非常糟糕的設計,絕對不應該使用。它只是吸引了很多方面。我建議儘可能不要使用它。
如果是我,我會使用Vista中引入的新的通用文件項目對話框,這是本機文件夾選取器 –
要顯示根目錄名稱空間,您應該將'Root'參數設置爲空字符串(''' '')而不是''Desktop''。 –
謝謝@RemyLebeau!使用''''而不是''Desktop''解決了我的問題。 – user1627960