Delphi 7 - 保存到特定的.INI文件名 [英] Delphi 7 - Save to a Specific .INI Files Name

查看:131
本文介绍了Delphi 7 - 保存到特定的.INI文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了 MasterMan82的TIniFile代码,以从&到TEdit / TComboBox和TMemo。

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.

所以,我的意思是:

我有几个.INI文件, B.ini,C.ini ....等等。我只是把A.ini作为变量存储在代码中。不能将所有文件名放在代码中。

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.

当我打开A.ini时,进行一些更改,单击保存保存所做的任何更改,并成功!当然,因为A.ini已经在代码中定义了。

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.

但是,当我打开文件B.ini或C.ini或D.ini ...进行更改并保存,重新打开文件,但文件中的所有更改都已删除或未保存,当然,因为只有A.ini在代码中定义。

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 ?

以下是代码。 / p>

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;


推荐答案

打开ini文件时,将文件名存储在一个变量,在许多评论中解释。

When you open an ini file, store the filename in a variable as explained in many comments.

示例,( FCurrentIniFilename:String; 是TForm1中的私有变量) :

Example, (FCurrentIniFilename: String; is a private variable in TForm1):

FormCreate 事件中:

FCurrentIniFilename := ExtractFilePath(Application.EXEName)+ 'A.ini';
Read  := TINIFile.Create(FCurrentIniFilename);
...

在OpenFile事件中:

In the OpenFile event:

if OpenDialog.Execute then begin
  FCurrentIniFilename := OpenDialog.Filename;
  Open := TINIFile.Create(FCurrentIniFileName);
  try
    ...
  finally
    Open.Free;
  end;
end;

保存信息时:

ToSave := TINIFile.Create(FCurrentIniFilename);

这篇关于Delphi 7 - 保存到特定的.INI文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆