问题
I modified the MasterMan82's TIniFile code to read and write multi values from & to TEdit/TComboBox and TMemo.
Forgive my vague questions, my english is not good.
So, what I mean is:
I have a couple of .INI files, A.ini, B.ini, C.ini ....and so on. I just store A.ini as a variable in the code. It is not possible to put all the file names in the code.
When I opened A.ini, make some changes, click SAVE to save any changes made, and success!. Of course, because A.ini has been defined in the code.
However, when I open the file B.ini or C.ini or D.ini...making the change, and save, reopen the file, but all changes in the file is gone or not saved, of course, because only the A.ini was defined in the code.
So, my goal is how to keep or records all file revisions ?
Below is the code.
......
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, IniFiles, Dialogs;
......
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
I, LinesCount: Integer;
Read : TIniFile;
begin
Read := TINIFile.Create(ExtractFilePath(Application.EXEName)+ 'A.ini');
// Read := TIniFile.Create(ChangeFileExt(Application.Exename,'A.ini'));
Try
Proxy.Text := Read.ReadString('SETTING','Proxy','');
Port.Text := Read.ReadString('SETTING','Port','');
Route.Checked:= Read.ReadBool('SETTING','Route',False);
// TO READ MEMO LINES
LinesCount := Read.ReadInteger('MEMO', 'Lines Count', 0);
for I := 0 to LinesCount-1 do
Memo1.Lines.Insert(I, Read.ReadString('MEMO', 'Item'+IntToStr(I), ''));
Finally
Read.Free;
end;
end;
procedure TForm1.SaveClick(Sender: TObject);
var
I, LinesCount: Integer;
ToSave : TIniFile;
begin
ToSave := TINIFile.Create(ExtractFilePath(Application.EXEName)+ 'A.ini');
Try
ToSave.WriteString('SETTING','Proxy',Proxy.Text);
ToSave.WriteString('SETTING','Port',Port.Text);
ToSave.WriteBool('SETTING','Route',Route.Checked);
// TO SAVE MEMO LINES
LinesCount := Memo1.Lines.Count;
ToSave.WriteInteger('MEMO', 'Lines Count', LinesCount);
for I := 0 to LinesCount-1 do
ToSave.WriteString('MEMO', 'Item'+IntToStr(I), Memo1.Lines[I]);
Finally
ToSave.Free;
end;
end;
procedure TForm1.OpenClick(Sender: TObject);
var
I, LinesCount: Integer;
OpenFile : TIniFile;
begin
OpenDialog.Filter:='Ini File (.ini)|*.ini';
if OpenDialog.Execute then begin
Memo1.Clear;
OpenFile := TINIFile.Create(OpenDialog.FileName);
Try
Proxy.Text := OpenFile.ReadString('SETTING','Proxy','');
Port.Text := OpenFile.ReadString('SETTING','Port','');
Route.Checked:= OpenFile.ReadBool('SETTING','Route',False);
// TO READ MEMO LINES
LinesCount := OpenFile.ReadInteger('MEMO', 'Lines Count', 0);
for I := 0 to LinesCount-1 do
Memo1.Lines.Insert(I, OpenFile.ReadString('MEMO', 'Item'+IntToStr(I), ''));
Finally
OpenFile.Free;
end;
end;
end;
回答1:
When you open an ini file, store the filename in a variable as explained in many comments.
Example, (FCurrentIniFilename: String;
is a private variable in TForm1):
In the FormCreate
event:
FCurrentIniFilename := ExtractFilePath(Application.EXEName)+ 'A.ini';
Read := TINIFile.Create(FCurrentIniFilename);
...
In the OpenFile event:
if OpenDialog.Execute then begin
FCurrentIniFilename := OpenDialog.Filename;
Open := TINIFile.Create(FCurrentIniFileName);
try
...
finally
Open.Free;
end;
end;
When you are saving the information:
ToSave := TINIFile.Create(FCurrentIniFilename);
回答2:
You're expecting there to be magic where none exists. If you want to save to the same file you opened, then store the chosen name in a variable when you open it, and then use that variable when you save, too.
Likewise, if you want to remember the name from one run to the next, then you need to store the name in persistent storage (like the registry or an INI file), and then read that name when your program starts next time.
It's not difficult to get what you've requested, but you'll have to write some code for it.
来源:https://stackoverflow.com/questions/30722521/delphi-7-save-to-a-specific-ini-files-name