INI files play a very important role in system configuration and application parameter saving and setting. Therefore, the visual programming family, such as VB, VC, VFP, Delphi, etc., provide methods for reading and writing INI files. Among them, INI is operated in Delphi. file, the most concise, this is because Delphi provides a TInifile class, which allows us to process INI files very flexibly . INI file structure [Section name] ini file keyword 1 = value 1 Keyword 2 = value 2 INI files allow multiple sections, and each section allows multiple keywords. "=" is followed by the value of the keyword. There are three types of values: strings, integer values, and Boolean values. The string is stored in the INI file without quotation marks, the Boolean true value is represented by 1, and the Boolean false value is represented by 0.
2. Definition
1. Add IniFiles in the Uses section of the Interface;
2. Add a line in the Var variable definition section: myinifile:Tinifile;
Defines an instance of the class. Then, you can create, open, read, write and other operations on the variable myinifile.
3. Open the INI file
myinifile:=Tinifile.create(PRogram.ini);
The above line of statement will establish a connection between the variable myinifile and the specific file program.ini. Then, the value of the keywords in the program.ini file can be read and written through the variable myinifile. It is worth noting that if the file name in brackets does not specify a path, then the Program.ini file will be stored in the Windows directory. The method to store the Program.ini file in the current directory of the application is to specify a complete Path and file name. The following two statements can complete this function: Filename:=ExtractFilePath(Paramstr(0))+program.ini;myinifile:=Tinifile.Create(filename);
5. Write INI file
Similarly, the TInifile class also provides three different object methods to write strings, integers, and Boolean type keywords to the INI file.
myinifile.writestring(section name, keyword, variable or string value);
myinifile.writeinteger(section name, keyword, variable or integer value);
myinifile.writebool(section name, keyword, variable or True or False);
When the INI file does not exist, the above statement will automatically create the INI file.
6. Delete keywords
In addition to adding a keyword using the write method, the Tinifile class also provides an object method for deleting keywords:
myinifile.DeleteKey(section name,keyword);
7. Section operation
Adding a section can be done with the write method, and deleting a section can be done with the following object method:
myinifile.EraseSection(section name);
In addition, the Tinifile class also provides three object methods to operate on sections: myinifile.readsection (section name, TStrings variable); all key names in the specified section can be read into a string list variable; myinifile.readsections (TStrings variable); can read all the section names in the INI file into a string list variable. myinifile.readsectionvalues (section name, TStrings variable); can read all lines (including keywords, =, values) of the specified section in the INI file into a string list variable.
8. Release
Release myinifile in the appropriate location with the following statement:
myinifile.distory;
Here are specific examples. The source code is as follows. A myini.ini file was created, with a section named newini and three keywords: "user name", "elapsed running time" and "whether it is an official user". For the running effect, you can fill in the "user name" in edit1; edit2 displays the time, and the value cannot be changed; checkbox1 saves the time and "user name" by ticking it into the myini.ini file. When the application is reopened, the If the time saved and the "user name" filled in are modified in the myini.ini file, the effect will be the same as when modified while the program is running.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,inifiles, StdCtrls, ExtCtrls; {call inifiles class}type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; Edit2: TEdit; Timer1: TTimer; CheckBox1: TCheckBox; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationvarmyinifile:TInifile;{ Define an instance of a class}{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);varfilename:string;begin{The following two lines are written in the form of creating an ini file under the path of the application}filename:=ExtractFilePath(paramstr(0))+'myini.ini';myinifile:=TInifile.Create( filename);edit1.Text:=myinifile.ReadString(' newini','User name','Hu Changhao');edit2.text:=inttostr(myinifile.readinteger('newini','Elapsed running time',0));checkbox1.Checked:=myinifile.readbool('newini' ,'Is it an official user',False);{newini is the section name, the middle field is the name of the keyword, and the third field is the default value. When myini.ini does not exist, the above statement automatically creates this file. The quotation marks in the above lines are single quotation marks}end;procedure TForm1.FormDestroy(Sender: TObject);begin myinifile.writestring('newini','user name',edit1.Text);myinifile.writeinteger('newini','Elapsed running time',strtoint(edit2.text));myinifile.writebool('newini','whether Official user',checkbox1.Checked);myinifile.Destroy;end;procedure TForm1.Timer1Timer(Sender: TObject);begin edit2.Text:=inttostr(strtoint(edit2.text)+1);end;end.
ini file