Download / save .ini file

I wrote code that allows me to store variables in a .ini file with a custom file name. The file name depends on the text in EditBox1. No problems. My problem is how to load these variables from this user-defined file name by selecting a file from the download file window (Windows Explorer).

DataIni:=TIniFile.Create(getcurrentdir+'\save folder\' + EditBox1.Text +'.ini');

Saving Variable Example

DataIni.WriteString('INFO','FirstName',EditFirstName.Text);
DataIni.WriteString('INFO','MiddleName',EditMiddleName.Text);
DataIni.WriteString('INFO','FamilyName',EditFamilyName.Text);

Variable load example

EditFirstName.Text := DataIni.ReadString('INFO','FirstName',EditFirstName.Text);
EditMiddleName.Text := DataIni.ReadString('INFO','MiddleName',EditMiddleName.Text);
EditFamilyName.Text := DataIni.ReadString('INFO','FamilyName',EditFamilyName.Text);

So, for example EditBox1.Text = 'MyFile1', how to load the variables stored in MyFile1.ini by selecting MyFile1.ini from the list of other files?

+4
source share
1 answer

TOpenDialog, , , .

uses IniFiles;
...

, OpenDialog.Execute, .

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TOpenDialog.Create(Self) do
  try
    Filter := 'Ini Files (*.ini)|*.ini';
    if Execute then
      LoadMyVars(FileName);
  finally
    Free;
  end;
end;

ini .

procedure TForm1.LoadMyVars(const AFileName: string);
var
  DataIni : TIniFile;
begin
  DataIni := TIniFile.Create(AFileName);
  try
    EditFirstName.Text := DataIni.ReadString('INFO','FirstName', '');
    EditMiddleName.Text := DataIni.ReadString('INFO','MiddleName', '');
    EditFamilyName.Text := DataIni.ReadString('INFO','FamilyName', '');
  finally
    FreeAndNil(DataIni);
  end;
end;
0

Source: https://habr.com/ru/post/1627769/


All Articles