23
votes

Usually, with Windows, I save my application's data in the user folder (%appdata%).

For that, I use the function ExpandEnvironmentStrings which is linked to Windows to get the folder I need, and I store inside a subfolder my inifile.

Is there any best practice to manage that and be compliant with all the supported platforms (Windows 32b, 64b & Mac)?


I successfully tested like that:

procedure TfrmMain.SaveSettings;
var
  fnINI: TFileName;
  ini  : TIniFile;
begin
  fnINI := IncludeTrailingPathDelimiter(GetHomePath) + IncludeTrailingPathDelimiter(APP_NAME) + ChangeFileExt(APP_NAME, '.ini');
  if ForceDirectories(ExtractFilePath(fnINI)) then
  begin
    ini := TIniFile.Create(fnINI);
    try
      ini.WriteString(INI_CONNECTION, INI_IP, edtIP.Text);
    finally
      ini.Free;
    end;
  end;
end;
1
Please move your findings to an answer of their own rather than editing your question. That way people can vote on the answer and question separately.LachlanG
@LachlanG: In this case I disagree. Whiler added an example showing use of the answer he accepted. IMHO he shouldn't add another answer to do so, but did the right thing in updating his question to include his verification that the answer is correct.lkessler
@lkessler: Why not just add a comment to the answer saying he's verified it works? He already left a comment saying he would check it for himself. Also rather than add his solution code to his question, why not edit Linas's answer and place the code there.LachlanG
@LachlanG: The comment would have been appropriate if he didn't have the example. But examples can't format nicely in comments. And IMHO it is better to edit your own question with your own interpretation of the answer than to edit someone else's answer.lkessler
@lkessler: This is exactly why I did it like that...Whiler

1 Answers

22
votes

Haven't tried XE2 but probably you could use SysUtils.GetHomePath. Also check IOUtils where you can find useful records (TFile, TPath, TDirectory) for managing files, paths and directories. They should support different platforms.