2008. május 16., péntek

Get special Windows folder location


Problem/Question/Abstract:

How to get special Windows folder location

Answer:

Solve 1:

uses
...ShlObj...

var
SFolder: pItemIDList;
SpecialPath: array[0..MAX_PATH] of Char;
begin
        SHGetSpecialFolderLocation(Form1.Handle, CSIDL_STARTUP, SFolder);
        SHGetPathFromIDList(SFolder, SpecialPath);
  Label1.Caption := StrPas(SpecialPath);
end;

Other folders :

CSIDL_BITBUCKET
CSIDL_CONTROLS
CSIDL_DESKTOP - WINDOWS\Desktop
CSIDL_DESKTOPDIRECTORY - WINDOWS\Desktop
CSIDL_DRIVES
CSIDL_FONTS - WINDOWS\FONTS
CSIDL_NETHOOD - WINDOWS\NetHood
CSIDL_NETWORK
CSIDL_PERSONAL - X:\My Documents
CSIDL_PRINTERS
CSIDL_PROGRAMS - WINDOWS\StartMenu\Programs
CSIDL_RECENT - WINDOWS\Recent
CSIDL_SENDTO - WINDOWS\SendTo
CSIDL_STARTMENU - WINDOWS\Start Menu
CSIDL_STARTUP - WINDOWS\Start Menu\Programs\StartUp
CSIDL_TEMPLATES - WINDOWS\ShellNew


Solve 2:

function WinAPI_GetWindowsDirectory: string;
begin
  SetLength(Result, MAX_PATH);
  SetLength(Result, GetWindowsDirectory(pchar(Result), MAX_PATH));
end;

function WinAPI_GetSystemDirectory: string;
begin
  SetLength(Result, MAX_PATH);
  SetLength(Result, GetSystemDirectory(pchar(Result), MAX_PATH));
end;

In fact you can get just about every special folder location

function WinAPI_SHGetSpecialFolderLocation(nFolder: integer): string;
var
  pidl: PItemIDList;
begin
  SHGetSpecialFolderLocation(0, nFolder, pidl);
  SetLength(Result, MAX_PATH);
  SHGetPathFromIDList(pidl, pchar(Result));
  SetLength(Result, pchar_StrLen(pchar(Result)));
end;


Solve 3:

function GetSystemPath(Folder: Integer): string;
var
  PIDL: PItemIDList;
  Path: LPSTR;
  AMalloc: IMalloc;
begin
  Path := StrAlloc(MAX_PATH);
  SHGetSpecialFolderLocation(Application.Handle, Folder, PIDL);
  if SHGetPathFromIDList(PIDL, Path) then
    Result := Path;
  SHGetMalloc(AMalloc);
  AMalloc.Free(PIDL);
  StrDispose(Path);
end;

Nincsenek megjegyzések:

Megjegyzés küldése