我有这个单位将设置保存到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上无法使用?调用UpdateFile
System.IniFiles
单位中的过程的保存设置也是正确的方法吗?
答案 0 :(得分:6)
将设置保存在与任何平台上安装的应用程序相同的文件夹中是一种不好的做法,很可能此文件夹将受到保护,不会为普通用户编写(例如,请考虑Windows上的“程序文件”)。 正确的方法是使用System.IOUtils单元中的TPath.GetHomePath文件夹。 因此,您应该使用以下行来创建Settings对象。
Settings := TSettings.Create(TPath.Combine(TPath.GetHomePath, 'YourAppSettings.ini'));
答案 1 :(得分:1)
如果要将文件保存到应用程序工作数据目录(除了在应用程序运行目录上执行此操作外,您应该使用GetHomePath + '/settings.ini';
。有关GetHomePath和TMemIniFile的详细信息。最好的问候,希望它有所帮助!!!