Delphi Code - Get the Location for Windows Special Folders
Here is a simple Delphi function that retrieves the location of Windows special folders, such as desktop, start menu, program files, fonts, AppData, etc. The function is useful when attempting to search within the special folders or to
create a shortcut link (e.g., a link on the Windows desktop). For additional Delphi code for the Windows operating system, see
Useful Delphi Code for the Windows Operating System.
function WindowsSpecialFolderLocation(FolderID: Integer): String;
var
PIDL: PItemIDList; //uses ShlObj unit
InFolder: Array[0..MAX_PATH-1] of Char;
begin
WindowsSpecialFolderLocation := '';
try
SHGetSpecialFolderLocation(0, FolderID, PIDL);
If Assigned(PIDL) then
begin
If SHGetPathFromIDList(PIDL, InFolder) = True then WindowsSpecialFolderLocation := String(InFolder);
end;
except
WindowsSpecialFolderLocation := '';
end;
end;
The procedure below uses the function shown above to get the location of a few Windows special folders.
procedure TForm1.Button1(Sender: TObject);
var
Desktop, CommonDesktop, MyDocuments, CommonDocuments, ProgramsFiles, StartMenu, CommonStartMenu, Programs, CommonPrograms, Startup, Recent, Favorites, Fonts, AppData, LocalAppData, CommonAppData: String;
begin
//variables should always be properly initialized
Desktop := '';
CommonDesktop := '';
MyDocuments := '';
CommonDocuments := '';
ProgramsFiles := '';
StartMenu := '';
CommonStartMenu := '';
Programs := '';
CommonPrograms := '';
Startup := '';
Recent := '';
Favorites := '';
Fonts := '';
AppData := '';
LocalAppData := '';
CommonAppData := '';
//folder IDs are in the ShlObj unit
Desktop := WindowsSpecialFolderLocation(CSIDL_DESKTOP);
CommonDesktop := WindowsSpecialFolderLocation(CSIDL_COMMON_DESKTOPDIRECTORY);
MyDocuments := WindowsSpecialFolderLocation(CSIDL_MYDOCUMENTS);
CommonDocuments := WindowsSpecialFolderLocation(CSIDL_COMMON_DOCUMENTS);
ProgramsFiles := WindowsSpecialFolderLocation(CSIDL_PROGRAM_FILES);
StartMenu := WindowsSpecialFolderLocation(CSIDL_STARTMENU);
CommonStartMenu := WindowsSpecialFolderLocation(CSIDL_COMMON_STARTMENU);
Programs := WindowsSpecialFolderLocation(CSIDL_PROGRAMS);
CommonPrograms := WindowsSpecialFolderLocation(CSIDL_COMMON_PROGRAMS);
Startup := WindowsSpecialFolderLocation(CSIDL_STARTUP);
Recent := WindowsSpecialFolderLocation(CSIDL_RECENT);
Favorites := WindowsSpecialFolderLocation(CSIDL_FAVORITES);
Fonts := WindowsSpecialFolderLocation(CSIDL_FONTS);
AppData := WindowsSpecialFolderLocation(CSIDL_APPDATA);
LocalAppData := WindowsSpecialFolderLocation(CSIDL_LOCAL_APPDATA);
CommonAppData := WindowsSpecialFolderLocation(CSIDL_COMMON_APPDATA);
end;
Below is a non-exhaustive list of defined constants for special folders. The values for special folders (CSIDL, constant special item ID list) can be found in the
ShlObj unit. For additional information, please see the
Microsoft CSIDL page.
CSIDL_DESKTOP
CSIDL_INTERNET
CSIDL_PROGRAMS
CSIDL_CONTROLS
CSIDL_PRINTERS
CSIDL_PERSONAL
CSIDL_FAVORITES
CSIDL_STARTUP
CSIDL_RECENT
CSIDL_SENDTO
CSIDL_BITBUCKET
CSIDL_STARTMENU
CSIDL_MYDOCUMENTS
CSIDL_MYMUSIC
CSIDL_MYVIDEO
CSIDL_DESKTOPDIRECTORY
CSIDL_DRIVES
CSIDL_NETWORK
CSIDL_NETHOOD
CSIDL_FONTS
CSIDL_TEMPLATES
CSIDL_COMMON_STARTMENU
CSIDL_COMMON_PROGRAMS
CSIDL_COMMON_STARTUP
CSIDL_COMMON_DESKTOPDIRECTORY
CSIDL_APPDATA
CSIDL_PRINTHOOD
CSIDL_LOCAL_APPDATA
CSIDL_ALTSTARTUP
CSIDL_COMMON_ALTSTARTUP
CSIDL_COMMON_FAVORITES
CSIDL_INTERNET_CACHE
CSIDL_COOKIES
CSIDL_HISTORY
CSIDL_COMMON_APPDATA
CSIDL_WINDOWS
CSIDL_SYSTEM
CSIDL_PROGRAM_FILES
CSIDL_MYPICTURES
CSIDL_PROFILE
CSIDL_SYSTEMX86
CSIDL_PROGRAM_FILESX86
CSIDL_PROGRAM_FILES_COMMON
CSIDL_PROGRAM_FILES_COMMONX86
CSIDL_COMMON_TEMPLATES
CSIDL_COMMON_DOCUMENTS
CSIDL_COMMON_ADMINTOOLS
CSIDL_ADMINTOOLS
CSIDL_CONNECTIONS
CSIDL_COMMON_MUSIC
CSIDL_COMMON_PICTURES
CSIDL_COMMON_VIDEO
CSIDL_RESOURCES
CSIDL_RESOURCES_LOCALIZED
CSIDL_COMMON_OEM_LINKS
CSIDL_CDBURN_AREA
CSIDL_COMPUTERSNEARME
CSIDL_FLAG_CREATE
CSIDL_FLAG_DONT_VERIFY
CSIDL_FLAG_DONT_UNEXPAND
CSIDL_FLAG_NO_ALIAS
CSIDL_FLAG_PER_USER_INIT
CSIDL_FLAG_MASK
CSIDL_PROFILES
Posted: Saturday, September 23, 2023