Storing application data in Windows XP and higher

By | March 2, 2013
uses ShlObj;

function GetSpecialFolder(const CSIDL: integer) : string;
var
  RecPath : PWideChar;
begin
  RecPath := StrAlloc(MAX_PATH);
    try
    FillChar(RecPath^, MAX_PATH, 0);
    if SHGetSpecialFolderPath(0, RecPath, CSIDL, false) 
      then result := RecPath
      else result := '';
    finally
      StrDispose(RecPath);
    end;
end;

And I call it with

GetSpecialFolder(CSIDL_APPDATA)

Where the list of CDISL is defined here.

GetSpecialFolder(CSIDL_APPDATA) returns C:\Users\username\AppData\Roaming in Windows

List of CDISL available at http://msdn.microsoft.com/en-us/library/bb762494%28v=vs.85%29.aspx

Most useful ones will probably be…

CSIDL_APPDATA
FOLDERID_RoamingAppData
Version 4.71. The file system directory that serves as a common repository for application-specific data. A typical path is C:\Documents and Settings\username\Application Data. This CSIDL is supported by the redistributable Shfolder.dll for systems that do not have the Internet Explorer 4.0 integrated Shell installed.
CSIDL_COMMON_APPDATA
FOLDERID_ProgramData
Version 5.0. The file system directory that contains application data for all users. A typical path is C:\Documents and Settings\All Users\Application Data. This folder is used for application data that is not user specific. For example, an application can store a spell-check dictionary, a database of clip art, or a log file in the CSIDL_COMMON_APPDATA folder. This information will not roam and is available to anyone using the computer.
CSIDL_COMMON_DESKTOPDIRECTORY
FOLDERID_PublicDesktop
The file system directory that contains files and folders that appear on the desktop for all users. A typical path is C:\Documents and Settings\All Users\Desktop.
CSIDL_COMMON_DOCUMENTS
FOLDERID_PublicDocuments
The file system directory that contains documents that are common to all users. A typical path is C:\Documents and Settings\All Users\Documents.
CSIDL_LOCAL_APPDATA
FOLDERID_LocalAppData
Version 5.0. The file system directory that serves as a data repository for local (nonroaming) applications. A typical path is C:\Documents and Settings\username\Local Settings\Application Data.
CSIDL_PROFILE
FOLDERID_Profile
Version 5.0. The user’s profile folder. A typical path is C:\Users\username. Applications should not create files or folders at this level; they should put their data under the locations referred to by CSIDL_APPDATA or CSIDL_LOCAL_APPDATA. However, if you are creating a new Known Folder the profile root referred to by CSIDL_PROFILE is appropriate.
CSIDL_PROGRAM_FILES
FOLDERID_ProgramFiles
Version 5.0. The Program Files folder. A typical path is C:\Program Files.