我的任务听起来很简单...所以我想。
任务:仅对本地驱动器使用 FileCtrl.SelectDirectory 对话框。不允许显示映射的驱动器、网络、共享和其他远程路径。
看起来打开带有根 = 我的电脑虚拟文件夹的对话框是个好主意。
但是当我尝试不同的方法来获取路径时,结果总是得到空字符串。
请告诉我我做错了什么?
在下面的示例中,我展示了我在 Web 中非常流行的 2 种方法。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, cxClasses, cxShellBrowserDialog, FileCtrl, ShlObj, KnownFolders,
ActiveX;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetKnownFolderPath(const folder : KNOWNFOLDERID ) : string;
var
path: LPWSTR;
begin
if SUCCEEDED(SHGetKnownFolderPath(folder, 0, 0, path)) then
begin
try
Result := path;
finally
CoTaskMemFree(path);
end;
end else
Result := '';
end;
function GetSpecialFolderPath(CSIDLFolder: Integer): string;
var
FilePath: array [0..MAX_PATH] of char;
begin
SHGetFolderPath(0, CSIDLFolder, 0, 0, FilePath);
Result := FilePath;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Root, Directory: String;
begin
Root := GetKnownFolderPath(FOLDERID_ComputerFolder);
SelectDirectory('caption', Root, Directory, [sdNewUI, sdShowShares], nil);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Root, Directory: String;
begin
Root := GetSpecialFolderPath($0011); //CSIDL_DRIVES
SelectDirectory('caption', Root, Directory, [sdNewUI, sdShowShares], nil);
end;
end.
SelectDirectory()
仅适用于文件系统路径,而不适用于虚拟项目。 您尝试将Root
设置为没有文件系统路径的项目。
如果要显示适用于虚拟条目的对话框,则必须直接使用 SHBrowseForFolder()
,或改用IFile(Open)Dialog
。 然后,您可以使用分别由 PIDL
s 或 IShellItem
接口表示的项目。