将TiniFile从应用程序保存到iOS

时间:2013-10-22 11:39:04

标签: ios delphi firemonkey delphi-xe5

我是iOS平台的新手。我正在尝试为我的应用程序保存一个INI文件。问题是我无法获得具有写入权限的路径。

这是我的代码:

  ini := TIniFile.Create(GetHomePath + '/user.dat');
  try
    ini.WriteString('data','user',edtuser.Text);
    ini.WriteString('data','descr',edt1.Text);
  finally
    ini.Free;
  end;

我得到一个例外,即无法创建该文件。如何使用Firemonkey获取可写路径?

2 个答案:

答案 0 :(得分:12)

使用TPath.GetDocumentsPath(并使用TPath.Combine代替串联,删除硬编码的/):

uses
  Systen,IOUtils;


ini := TIniFile.Create(TPath.Combine(TPath.GetDocumentsPath, 'user.dat'));

使用TPath.GetDocumentsPath可以透明地在所有支持的平台(Win32,Win64,OSX,iOS和Android)上运行,使用TPath.Combine会自动添加TPath.DirectorySeparatorChar,因此您不会必须手动连接它们。

如果你愿意自己做,但是:

var
  IniName: string;
begin
  IniName := TPath.GetDocumentsPath + TPath.DirectorySeparatorChar + 'user.dat';
  Ini := TIniFile.Create(IniName);
  try
    // Rest of code
  finally
    Ini.Free;
  end;
end;

答案 1 :(得分:2)

可以thisthis可以帮助您

uses INIFiles;

function TForm6.MyINIFilePath: string;
begin
//  Result := GetHomePath + PathDelim + 'Library' + PathDelim+'My.ini';
  Result := GetHomePath + PathDelim + 'Documents' + PathDelim+'MyD.ini';
end;