1. It is necessary to understand the structure of the INI file:
;comment
[section name]
keyword=value
...
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.
Comments start with a semicolon ";".
2. Definition
1. Add IniFiles in the Uses section of the Interface;
2. Add a line to the Var variable definition section:
myinifile:Tinifile;
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 accomplish this function
able:
Filename:=ExtractFilePath(Paramstr(0))+'program.ini';
myinifile:=Tinifile.Create(filename);
4. Read the value of the keyword
For the three data types of strings, integer values, and Boolean values supported by INI files, the TINIfiles class provides three different object methods to read the values of keywords in INI files.
Assume that the defined variables vs, vi, and vb are string, integer, and boolean types respectively.
vs:=myinifile.Readstring('section name','keyword',default value);
vi:=myinifile.Readinteger('section name','keyword',default value);
vb:=myinifile.Readbool('section name','keyword',default value);
The default value is the default value returned when the keyword does not exist in the INI file.
5. Write INI file
Similarly, the TInifile class also provides three different object methods to write string, integer, 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); can read all keyword names in the specified section 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;
9. An example
The following uses a simple example (as shown in the figure) to demonstrate the method of creating, reading, and storing INI files. The myini.ini file contains the "Program Parameters" section, and three keywords: user name (string), official user status (Boolean value), and elapsed time (integer value). The program reads this data when the form is created and writes the myini.ini file when the form is released.
Attached source program list
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,IniFiles,StdCtrls,ExtCtrls;
type
TForm1=class(TForm)
Edit1:TEdit;
Edit2:TEdit;
CheckBox1:TCcheckBox;
Timer1:TTimer;
procedureFormCreate(Sender:TObject);
procedureFormDestroy(Sender:TObject);
procedureTimer1Timer(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
myinifile:Tinifile;
implementation
{$R*.dfm}
procedureTForm1.FormCreate(Sender:TObject);
var
filename:string;
begin
filename:=ExtractFilePath(paramstr(0))+´myini.ini´;
myinifile:=TInifile.Create(filename);
edit1.Text:=myinifile.readstring(´program parameters´,´user name´,´default user name´);
edit2.text:=inttostr(myinifile.readinteger(´program parameters´,´elapsed running time´,0));
checkbox1.Checked:=myinifile.readbool(´program parameters´,´whether it is an official user´,False);
end;
procedureTForm1.FormDestroy(Sender:TObject);
begin
myinifile.writestring(´program parameters´,´user name´,edit1.Text);
myinifile.writeinteger(´program parameters´,´elapsed running time´,strtoint(edit2.text));
myinifile.writebool(´Program parameters´, ´Is it an official user´, checkbox1.Checked);
myinifile.Destroy;
end;
procedureTForm1.Timer1Timer(Sender:TObject);
begin
edit2.Text:=inttostr(strtoint(edit2.text)+1);
end;
end.
This example passed debugging under Delphi6.0+WinXP.