[函数] Delphi FMX Windows 取得下载 Downloads 目录

在 Firemonkey 提供了一个跨平台的函数 TPath.GetDownloadsPath 来取得该平台的下载目录,但是非常奇怪的是,在 Windows 平台下,取得的下载目录确是:

C:\Users\<username>\AppData\Local (官方文件

而不是

C:\Users\<username>\Downloads

也许 EMB 想的是 App 沙箱做法,但如果想要取得 C:\Users\<username>\Downloads 这个目录该如何做呢?

可以扩展 TPath 功能,参考如下:

unit System.IOUtils.Helper;

interface

uses
  {$IFDEF MSWINDOWS}
  Winapi.Windows,
  Winapi.ShlObj,
  Winapi.KnownFolders,
  {$ENDIF}

  System.IOUtils;

type
  TPathHelper = record helper for TPath
    class function GetWinDownloadsPath: string; static;
  end;

implementation

class function TPathHelper.GetWinDownloadsPath: string;
{$IFDEF MSWINDOWS}
var Path: PWideChar;
begin
     if Succeeded(SHGetKnownFolderPath(FOLDERID_Downloads, 0, 0, Path)) then
          Result := String(Path)
     else Result := '';
end;
{$ELSE}
begin
     Result := '';
end;
{$ENDIF}

end.

使用方法:

uses System.IOUtils.Helper;

var DownloadPsth: String;
begin
  DownloadPath := TPath.GetWinDownloadsPath;
end;

 

posted @ 2016-08-05 07:32  龟山Aone  阅读(1144)  评论(0编辑  收藏  举报