DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

GetDesktopFolder

Returns the path to the current user's desktop.

function GetDesktopFolder: string;
resourcestring
  sErrorMsg = 'Could not find Desktop folder location.';
begin
  if not IsSpecialFolderSupported(ShlObj.CSIDL_DESKTOP) then
    raise SysUtils.Exception.Create(sErrorMsg);
  Result := SpecialFolderPath(ShlObj.CSIDL_DESKTOP)
end;

GetKnownFolderPath

Returns the 'known folder path' specified by FolderID or the empty string if FolderID is not valid on the host operating system.

function GetKnownFolderPath(const FolderID: System.TGUID): UnicodeString;
var
  Buffer: PWideChar; // path returned by SHGetKnownFolderPath
begin
  Result := '';
  if ActiveX.Succeeded(ShlObj.SHGetKnownFolderPath(FolderID, 0, 0, Buffer)) then
    Result := Buffer;
  // According to MSDN, we must use CoTaskMemFree to free the string
  // allocated by this function and pointed to by ppszPath.
  ActiveX.CoTaskMemFree(Buffer);
end;

View the whole database.

Go to the DelphiDabbler website.