在Delphi取得桌面路徑

因為每台電腦的使用者不同,所以桌面的路徑也各不相同,Delphi又看不懂【%userprofile%\Desktop】這種路徑,因此要由Registry取得實際路徑,範例如下:


uses Registry;

function GetDeskTopDir: String;
var Root: TRegistry;
    aPath: String;
begin
  Root := TRegistry.Create;

  try
    Root.OpenKey('SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\EXPLORER\SHELL FOLDERS', False);
    aPath := Root.ReadString('DESKTOP');

    if RightStr(aPath, 1) <> '\' then
    begin
      aPath := aPath + '\';
    end;

    Result := aPath;
  finally
    Root.Free;
  end;
end;

熱門文章