2016-05-11 106 views
2

以下代碼在不同機器上獲得不同結果。一臺機器只提供桌面文件夾(不需要),另一臺機器提供桌面文件夾和計算機映射驅動器(需要)。SelectDirectory不包括某些機器上的驅動器

procedure TForm1.Button1Click(Sender: TObject); 
var 
    Directory : String; 
begin 
    FileCtrl.SelectDirectory('Caption', 'Desktop', Directory, [sdNewUI, sdShowEdit]); 
end; 

其中一臺機器它給出:

Bad Browse

在另一方面它給:

Good Browse

這感覺就像一個窗口的設置,但我不知道在哪裏開始。使用德爾福XE,Windows 10.

任何想法表示讚賞。謝謝你的時間。

+1

如果是我,我會使用Vista中引入的新的通用文件項目對話框,這是本機文件夾選取器 –

+0

要顯示根目錄名稱空間,您應該將'Root'參數設置爲空字符串(''' '')而不是''Desktop''。 –

+0

謝謝@RemyLebeau!使用''''而不是''Desktop''解決了我的問題。 – user1627960

回答

3

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

enter image description here

現在你有一個對話框:

  • 始終工作。
  • 允許複製粘貼

*)不與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是微軟公司的一個非常糟糕的設計,絕對不應該使用。它只是吸引了很多方面。我建議儘可能不要使用它。

+0

* app *仍然可以在XP上運行,但*對話框*不會(在運行時調用對話框時引發'EPlatformVersionException')。 –

+0

@RemyLebeau,感謝您的幫助。我沒有XP來測試,所以我不確定會發生什麼。鑑於它確實從XP開始,我已經添加了一些解決方法代碼。 – Johan

+0

'如果WinVer在......' - OUCH!如果使用Win32MajorVersion <6然後...'來代替它會容易得多。並使用'try/finally'來釋放'TFileOpenDialog'。 –

相關問題