下面的代码在不同的机器上得到不同的结果。一台机器只提供桌面文件夹(不需要),另一台机器提供桌面文件夹和计算机、映射驱动器(需要)。
procedure TForm1.Button1Click(Sender: TObject);
var
Directory : String;
begin
FileCtrl.SelectDirectory('Caption', 'Desktop', Directory, [sdNewUI, sdShowEdit]);
end;
一机多用,它提供:
在另一个方面它给出:
这感觉像是 Windows 设置,但我不知道从哪里开始。使用Delphi XE,Windows 10。
任何想法都值得赞赏。感谢您抽出时间。
最佳答案
解决方法
使用 TFileOpenDialog
相反*。
设置FileOpenDialog1.Options:= [fdoPickFolders,fdoPathMustExist]
现在您有一个对话框:
- 始终有效。
- 允许复制粘贴
*) 不要与 TOpenDialog 混淆,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 [in]
Type: LPWSTR
A null-terminated Unicode string with the display name. Because each Shell folder defines its own parsing syntax, the form this string can take may vary. The desktop folder, for instance, accepts paths such as "C:\My Docs\My File.txt". It also will accept references to items in the namespace that have a GUID associated with them using the "::{GUID}" syntax.
请注意,文档指出桌面文件夹将接受路径和指南。它不接受'Desktop'
。因为那两者都不是。
事实上'Desktop'
作为root
适用于一个系统,但不适用于另一个系统,这是在 IDesktopFolder
的旧/新版本中进行的一些未记录的修复。界面。
技术方案
使用''
作为“根”,如我上面的代码所示。
显然SelectDirectory
是微软的一个非常糟糕的设计,永远不应该使用。它在很多方面都很糟糕。我建议尽可能不要使用它。
关于windows - SelectDirectory 不包括某些计算机上的驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37166483/