23

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;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Whiler
  • 7,998
  • 4
  • 32
  • 56
  • 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 Sep 17 '11 at 00:50
  • 1
    @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 Sep 27 '11 at 21:42
  • @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 Sep 30 '11 at 08:14
  • 1
    @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 Sep 30 '11 at 18:35
  • @lkessler: This is exactly why I did it like that... – Whiler Oct 01 '11 at 02:15

1 Answers1

22

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.

Linas
  • 5,485
  • 1
  • 25
  • 35