下列方法僅提供 Windows 平台使用,所以需要使用編譯開關,代碼如下:
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
Winapi.SHFolder,
{$ENDIF}
{$IFDEF MSWINDOWS}
const CSIDL_DESKTOP = $0000; // <desktop>
function GetDesktopPath: string;
var
LStr: array[0 .. MAX_PATH] of Char;
begin
SetLastError(ERROR_SUCCESS);
if SHGetFolderPath(0, CSIDL_DESKTOP, 0, 0, @LStr) = S_OK then
Result := LStr;
end;
{$ENDIF}