トップへ(mam-mam.net/)

Get the Full Path of Windows Special Folders in Delphi | SHGetSpecialFolderPath Guide

Japanese

Get the Full Path of Windows Special Folders in Delphi

This section explains how to retrieve the full path of Windows special folders—such as Desktop, My Documents, Application Data, and more—using the SHGetSpecialFolderPath API in Delphi. Special folders are system‑defined locations that applications commonly access, and knowing how to obtain their absolute paths is essential for file operations, configuration storage, and user‑specific data handling.

SHGetSpecialFolderPath Function

This section explains how to use the Windows API function SHGetSpecialFolderPath to retrieve the full path of a Windows special folder.

function SHGetSpecialFolderPath(hwndOwner: HWND; lpszPath: PWideChar; nFolder: Integer; fCreate: LongBool): LongBool;

Return Value
Returns TRUE on success, or FALSE on failure.
hwndOwner
The handle of the owner window.
lpszPath
The buffer that receives the retrieved folder path.
nFolder
A CSIDL value that specifies which special folder to retrieve.
fCreate
If TRUE, the folder is created if it does not already exist.

CSIDL Values for nFolder

CSIDL_DESKTOP
The user's Desktop folder.
(Example) C:\Users\<User Name>\Desktop
CSIDL_FAVORITES
The user's Favorites folder.
(Example) C:\Users\<User Name>\Favorites
CSIDL_FONTS
The system Fonts folder.
(Example) C:\Windows\Fonts
CSIDL_PERSONAL
The user's Documents folder (same as CSIDL_MYDOCUMENTS).
(Example) C:\Users\<User Name>\Documents
CSIDL_PROGRAM_FILES
The Program Files directory.
(Example) C:\Program Files
CSIDL_PROGRAMS
The Programs folder shown in the user's Start Menu.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
CSIDL_STARTUP
The user's Startup folder. Shortcuts placed here run automatically when the user logs in.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
CSIDL_WINDOWS
The Windows directory (%windir% or %SYSTEMROOT%).
(Example) C:\Windows
CSIDL_SENDTO
The SendTo folder used by the right‑click “Send To” menu.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\SendTo
CSIDL_APPDATA
The user's roaming application data folder.
(Example) C:\Users\<User Name>\AppData\Roaming
CSIDL_TEMPLATES
The Templates folder.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Templates
CSIDL_COMMON_STARTMENU
The Start Menu folder shared by all users.
(Example) C:\ProgramData\Microsoft\Windows\Start Menu
CSIDL_COMMON_PROGRAMS
The Programs folder shared by all users.
(Example) C:\ProgramData\Microsoft\Windows\Start Menu\Programs
CSIDL_COMMON_STARTUP
The Startup folder shared by all users. Shortcuts placed here run for every user at login.
(Example) C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
CSIDL_COMMON_DESKTOPDIRECTORY
The Desktop folder shared by all users.
(Example) C:\Users\Public\Desktop
CSIDL_COMMON_APPDATA
The application data folder shared by all users.
(Example) C:\ProgramData
CSIDL_COMMON_DOCUMENTS
The public Documents folder.
(Example) C:\Users\Public\Documents
CSIDL_COMMON_PICTURES
The public Pictures folder.
(Example) C:\Users\Public\Pictures
CSIDL_COMMON_FAVORITES
The Favorites folder shared by all users.
(Example) C:\Users\<User Name>\Favorites
CSIDL_COMMON_ADMINTOOLS
The Administrative Tools folder shared by all users.
(Example) C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
CSIDL_ADMINTOOLS
The user's Administrative Tools folder.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
CSIDL_LOCAL_APPDATA
The user's local (non‑roaming) application data folder.
(Example) C:\Users\<User Name>\AppData\Local
CSIDL_SYSTEM
The Windows System directory.
(Example) C:\Windows\System32
CSIDL_PROGRAM_FILES_COMMON
The Common Files directory shared by applications.
(Example) C:\Program Files\Common Files
CSIDL_PROFILE
The user's profile folder.
(Example) C:\Users\<User Name>
CSIDL_RECENT
The folder containing shortcuts to the user's recently used documents.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Recent
CSIDL_PRINTHOOD
The folder containing printer shortcut objects.
(Example) C:\Users\<User Name>\AppData\Roaming\Microsoft\Windows\Printer Shortcuts

Example: Using SHGetSpecialFolderPath

To use SHGetSpecialFolderPath, you must include Winapi.ShlObj in the uses clause.
The following example retrieves the full path of the Desktop folder.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Winapi.ShlObj;

procedure TForm1.Button1Click(Sender: TObject);
var
  path: array[0..MAX_PATH-1] of Char;
  st: String;
begin
  ZeroMemory(@path[0], MAX_PATH * SizeOf(Char));

  SHGetSpecialFolderPath(0, path, CSIDL_DESKTOP, False);

  ShowMessage(path);
end;

end.