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

问题描述 投票:0回答:1

我的任务听起来很容易......所以我想。

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

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

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

请告诉我我做错了什么?

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

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.
delphi winapi delphi-2010
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.