在 Firemonkey 中将数据保存到 iniFile
Save data To iniFile in Firemonkey
我有这个装置可以将设置保存到 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
过程的保存设置也是正确的方法吗?
将设置保存在与安装在任何平台上的应用程序相同的文件夹中是一种不好的做法,很可能这个文件夹将被保护以防止普通用户写入(想想 'Program files' 在 Windows例如)。
正确的方法是使用 System.IOUtils 单元中的 TPath.GetHomePath 文件夹。
因此,您应该使用以下行来创建设置对象。
Settings := TSettings.Create(TPath.Combine(TPath.GetHomePath, 'YourAppSettings.ini'));
如果您想将文件保存到您的应用程序工作数据目录(除了在应用程序 运行 目录中执行此操作外,您应该这样做),您应该使用 GetHomePath + '/settings.ini';
。有关 GetHomePath and TMemIniFile 的更多信息。致以最诚挚的问候,希望对您有所帮助!!!
我有这个装置可以将设置保存到 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
过程的保存设置也是正确的方法吗?
将设置保存在与安装在任何平台上的应用程序相同的文件夹中是一种不好的做法,很可能这个文件夹将被保护以防止普通用户写入(想想 'Program files' 在 Windows例如)。 正确的方法是使用 System.IOUtils 单元中的 TPath.GetHomePath 文件夹。 因此,您应该使用以下行来创建设置对象。
Settings := TSettings.Create(TPath.Combine(TPath.GetHomePath, 'YourAppSettings.ini'));
如果您想将文件保存到您的应用程序工作数据目录(除了在应用程序 运行 目录中执行此操作外,您应该这样做),您应该使用 GetHomePath + '/settings.ini';
。有关 GetHomePath and TMemIniFile 的更多信息。致以最诚挚的问候,希望对您有所帮助!!!