delphi - 将数据保存到 Firemonkey 中的 iniFile

标签 delphi firemonkey delphi-xe8

我有这个单元可以将设置保存到iniFile

///////////////////////////////////////
// IniSettings.pas
//
// A class for saving settings (or whatever) in an .ini file.
//
// This is made in FireMonkey, but should be usable in Delphi too.
// Example of use in the bottum of the doc.

unit IniSettings;

interface

uses Inifiles;

type
  TSettings = class(TMemIniFile)
  private
    function GetSomeData: String;
    procedure SetSomeData(const Value: String);
    function GetFormTop: Integer;
    procedure SetFormTop(const Value: Integer);
    function GetFormLeft: Integer;
    procedure SetFormLeft(const Value: Integer);
    function GetFormHeight: Integer;
    function GetFormWidth: Integer;
    procedure SetFormHeight(const Value: Integer);
    procedure SetFormWidth(const Value: Integer);

  public
    procedure Save();
    property SomeData: String    read GetSomeData   write SetSomeData;
    property FormTop: Integer    read GetFormTop    write SetFormTop;
    property FormLeft: Integer   read GetFormLeft   write SetFormLeft;
    property FormWidth: Integer  read GetFormWidth  write SetFormWidth;
    property FormHeight: Integer read GetFormHeight write SetFormHeight;

  end;

var
  Settings: TSettings;

implementation

uses
  FMX.Forms, System.SysUtils;

{ TSettings }

// Save to the file
procedure TSettings.Save;
begin
  UpdateFile;
end;

// SomeData
function TSettings.GetSomeData: String;
begin
  result := ReadString('Settings', 'SomeData', 'default');
end;

procedure TSettings.SetSomeData(const Value: String);
begin
  WriteString('Settings', 'SomeData', Value);
end;

// FormTop
function TSettings.GetFormTop: Integer;
begin
  result := ReadInteger('Settings', 'FormTop', 10);
end;

procedure TSettings.SetFormTop(const Value: Integer);
begin
  WriteInteger('Settings', 'FormTop', Value);
end;

// FormLeft
function TSettings.GetFormLeft: Integer;
begin
  result := ReadInteger('Settings', 'FormLeft', 10);
end;

procedure TSettings.SetFormLeft(const Value: Integer);
begin
  WriteInteger('Settings', 'FormLeft', Value);
end;

// FormWidth
function TSettings.GetFormWidth: Integer;
begin
  result := ReadInteger('Settings', 'FormWidth', 640);
end;

procedure TSettings.SetFormWidth(const Value: Integer);
begin
  WriteInteger('Settings', 'FormWidth', Value);
end;

// FormHeight
function TSettings.GetFormHeight: Integer;
begin
  result := ReadInteger('Settings', 'FormHeight', 480);
end;

procedure TSettings.SetFormHeight(const Value: Integer);
begin
  WriteInteger('Settings', 'FormHeight', Value);
end;

initialization
  // Load/Create the 'settings.ini' file in the app folder
  Settings := TSettings.Create(ExtractFilePath(ParamStr(0)) + '/settings.ini');

finalization
  // Free the ini before closing
  FreeAndNil(Settings);

end.






///////////////////////////////////////
// Example
//
// Be sure to include the IniSettings in uses
//
// Put an TEdit(Edit1) and 3 TButton(btnLoad, btnSave and btnSaveFile) to the form
// The edit is for the 'SomeData' we're gonna save in the settings.
// One button for loading the settings from the MemIniFile, one for saving to MemIniFile
// and the last for saving the MemIniFile to the physical .ini file on the drive

uses
  IniSettings;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  Edit1.Text   := Settings.SomeData;
  Form1.Top    := Settings.FormTop;
  Form1.Left   := Settings.FormLeft;
  Form1.Width  := Settings.FormWidth;
  Form1.Height := Settings.FormHeight;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  Settings.SomeData   := Edit1.Text;
  Settings.FormTop    := Form1.Top;
  Settings.FormLeft   := Form1.Left;
  Settings.FormWidth  := Form1.Width;
  Settings.FormHeight := Form1.Height;
end;

procedure TForm1.btnSaveFileClick(Sender: TObject);
begin
  Settings.Save;
end; 

当我调用Settings.Save;时,应用程序强制关闭,并且我在设备上找不到iniFile,知道为什么我无法保存inifile 在设备上? TSettings.Create(ExtractFilePath(ParamStr(0)) 在 android 上不可用吗?还可以在 System.IniFiles 单元中调用 UpdateFile 过程来保存设置这是正确的做法吗?

最佳答案

将设置保存在与任何平台上安装的应用程序相同的文件夹中都是不好的做法,很可能该文件夹将受到保护,无法对普通用户进行写入(例如 Windows 上的“程序文件”)。 正确的方法是使用 System.IOUtils 单元中的 TPath.GetHomePath 文件夹。 因此,您应该使用以下行来创建 Settings 对象。

Settings := TSettings.Create(TPath.Combine(TPath.GetHomePath, 'YourAppSettings.ini'));

关于delphi - 将数据保存到 Firemonkey 中的 iniFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288528/

相关文章:

Delphi 访问修饰符

delphi - 如何从包含 D2007 的异常 block 中使用 Halt(n) 返回错误代码?

delphi - 如何在 Delphi 代码中获取 Advantage Database Server 安装的许可证计数

delphi - 如何获取内存中对象的数据集?

delphi - 自定义通知间隔

delphi - TCustomListbox 项目的自定义绘图

delphi - 功能 'rich' 为何是 FireMonkey 框架

android - XE8 Twebbrowser 本地 img 文件

delphi - 如何使用 TIdTCPServer 断开不活动客户端的连接?

javascript - TEmbeddedWB.ExecScriptEx 不适用于 JavaScript