以下代码在不同的计算机上得到不同的结果。一台计算机仅提供桌面文件夹(不需要),另一台计算机提供桌面文件夹和计算机,映射的驱动器(所需)。
procedure TForm1.Button1Click(Sender: TObject);
var
Directory : String;
begin
FileCtrl.SelectDirectory('Caption', 'Desktop', Directory, [sdNewUI, sdShowEdit]);
end;
一台机器可以提供:
另一方面,它给出:
感觉就像是Windows设置,但是我不确定从哪里开始。使用Windows 10的Delphi XE。
任何想法表示赞赏。谢谢你的时间。
解决方法
请TFileOpenDialog
改用*。
放FileOpenDialog1.Options:= [fdoPickFolders,fdoPathMustExist]
现在,您将看到一个对话框:
*)不要与TOpenDialog混淆,后者不允许您仅选择文件夹。
Solution for Windows XP
Note that the new TFileOpenDialog only works for Vista and above.
Your program will not work on XP if you include this control.
If you start the dialog on XP it will generate an EPlatformVersionException
.
You may want to use the following code instead if you want to be backward compatible:
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;
Recommended reading:
detect windows version
EDIT
FileCtrl.SelectDirectory('Caption', 'Desktop', Directory, [sdNewUI, sdShowEdit]);
The 'Desktop'
goes into the Root
parameter, which is handled like so:
...
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil,
Root, Eaten, RootItemIDList, Flags);
...
Here's what MSDN for IDesktopFolder.ParseDisplayName has to say:
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.
Note that the documentation states that the desktop folder will accept paths and guids. It does not accept 'Desktop'
. Because that's neither.
The fact that 'Desktop'
as a root
works on one system but not another is some undocumented fix made in an older/newer version of the IDesktopFolder
interface.
技术解决方案
使用''
的是在我上面的代码所示的“根”。
显然,这SelectDirectory
是Microsoft的一个非常糟糕的设计,永远不应该使用。它只是在很多方面都糟透了。我建议尽可能不要使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句