如何获取“我的电脑”虚拟文件夹路径?

时间:2019-04-09 14:23:53

标签: delphi winapi delphi-2010

我的任务听起来很简单...所以我想。

任务:仅将FileCtrl.SelectDirectory对话框与本地驱动器一起使用。 要显示映射的驱动器,不允许使用网络,共享和其他远程路径。

看起来最好使用Root = My Computer虚拟文件夹打开对话框。

但是当我尝试不同的方法来获取路径时,结果总是得到空字符串。

请告诉我我做错了什么?

在下面的示例中,我展示了我在网络上非常流行的两种方法。

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.

1 个答案:

答案 0 :(得分:0)

SelectDirectory()仅适用于文件系统路径,不适用于虚拟项目。您尝试将Root设置为没有文件系统路径的项目。

如果要显示适用于虚拟项目的对话框,则必须直接使用SHBrowseForFolder()或使用IFile(Open)Dialog。然后,您可以使用分别由PIDLIShellItem界面表示的项目。