有两种方法,一是用API:
uses
ShlObj;
function GetSysSPFolderPath(id: Integer): string;
var
s: string;
pIdList: PItemIDList;
begin
if SHGetSpecialFolderLocation(0, id, pIdList) = NOERROR then
begin
SetLength(s, MAX_PATH + 1);
SHGetPathFromIDList(pIdList, PChar(s));
Result := PChar(s);
end
else
Result := '';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := GetSysSPFolderPath(CSIDL_DESKTOP);
end;
另一个是查注册表:
const
RegRoot = '/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/';
with TRegistry.Create do
begin
try
if OpenKey(RegRoot, False) then
begin
Caption := ReadString('Desktop');
end;
finally
Free;
end;
end;