Store User and Application Data in the Correct Location

Get Known Folder Path Using Delphi

User / System Special Paths

When in the need of storing some content related to your Delphi application on the user's hard disk, you should take care of the support for state separation of user data, user settings, and computer settings.

For example, The "Application Data" folder in Windows should be used to store application-specific documents such as INI files, application state, temp files or similar.

You should never use hard-coded paths to specific locations, such as "c:\Program Files", as this may not work on other versions of Windows because the location of folders and directories can change with different versions of Windows.

The SHGetFolderPath Windows API function

The SHGetFolderPath is available in the SHFolder unit. SHGetFolderPath retrieves the full path of a known folder identified.

Here's a custom wrapper function around the SHGetFolderPath API to help you get any of the standard folders for all or the currently logged Windows user.

 uses SHFolder;
function GetSpecialFolderPath(folder : integer) : string;
const
  SHGFP_TYPE_CURRENT = 0;
var
  path: array [0..MAX_PATH] of char;
begin
  if SUCCEEDED(SHGetFolderPath(0,folder,0,SHGFP_TYPE_CURRENT,@path[0])) then
    Result := path
  else
    Result := '';
end;

Here's an example of using the SHGetFolderPath function:

  • Drop a TRadioButtonGroup (name: "RadioGroup1") on a form
  • Drop a TLabel (name: "Label1") on a form
  • Add 5 items to the radio group:
    1. "[Currenty User]\My Documents"
    2. "All Users\Application Data"
    3. "[User Specific]\Application Data"
    4. "Program Files"
    5. "All Users\Documents"
  • Handle the RadioGroup's OnClick event as:

Note: "[Current User]" is the name of the currently logged in Windows user.

 //RadioGroup1 OnClick
procedure TForm1.RadioGroup1Click(Sender: TObject) ;
var
  index : integer;
  specialFolder : integer;
begin
  if RadioGroup1.ItemIndex = -1 then Exit;
  index := RadioGroup1.ItemIndex;
  case index of
    //[Current User]\My Documents
    0: specialFolder := CSIDL_PERSONAL;
    //All Users\Application Data
    1: specialFolder := CSIDL_COMMON_APPDATA;
    //[User Specific]\Application Data
    2: specialFolder := CSIDL_LOCAL_APPDATA;
    //Program Files
    3: specialFolder := CSIDL_PROGRAM_FILES;
    //All Users\Documents
    4: specialFolder := CSIDL_COMMON_DOCUMENTS;
  end;
  Label1.Caption := GetSpecialFolderPath(specialFolder) ;
end;

Note: The SHGetFolderPath is a superset of SHGetSpecialFolderPath.

You should not store application-specific data (such as temporary files, user preferences, application configuration files, and so on) in the My Documents folder. Instead, use an application-specific file that is located in a valid Application Data folder.

Always append a subfolder to the path that SHGetFolderPath returns. Use the following convention: "\Application Data\Company Name\Product Name\Product Version".

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Store User and Application Data in the Correct Location." ThoughtCo, May. 18, 2017, thoughtco.com/store-user-and-application-data-in-the-correct-location-1058164. Gajic, Zarko. (2017, May 18). Store User and Application Data in the Correct Location. Retrieved from https://www.thoughtco.com/store-user-and-application-data-in-the-correct-location-1058164 Gajic, Zarko. "Store User and Application Data in the Correct Location." ThoughtCo. https://www.thoughtco.com/store-user-and-application-data-in-the-correct-location-1058164 (accessed April 19, 2018).