简体   繁体   中英

Save data To iniFile in Firemonkey

i have this unit that save settings to 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; 

When i call Settings.Save; the app force closes and i cannot find the iniFile on the device , any idea why i cannot save the inifile on the device ? is TSettings.Create(ExtractFilePath(ParamStr(0)) not usable on android ? also the save settings calling UpdateFile Procedure in System.IniFiles unit is this correct way to do ?

It is a bad practice to save settings in the same folder as application is installed on any platform, most likely this folder will be protected from writing for regular user (think 'Program files' on Windows for example). Correct way is to use TPath.GetHomePath folder from System.IOUtils unit. So you should use following line to create Settings object.

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

If you want to save a file to your apps working data directory (and you should, other than doing it on the app running directory), you should use GetHomePath + '/settings.ini'; . For more info on GetHomePath and TMemIniFile . Best regards and hope it helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM