Most applications in Windows have their own initialization files, such as PowerBuilder, Office, and Cstar. Therefore, reading and writing initialization files is a technology that every senior programmer must master. Although reading and writing initialization files can also be done using Object Text files in Pascal are read and written in the same way, but because the initialization file is different from ordinary text files, it has its own fixed format (see the initialization file below is the rdfnt.ini file provided in ucdos). If you use a text file to read Writing, format conversion is not only very tedious, but also prone to errors. In order to facilitate programmers to read and write data in the initialization file, Delphi provides users with a TIniFile class, through which the initialization file can be read and written very conveniently.
The contents of the rdfnt.ini file in Ucdos are:
[True Type fonts directory]
Dir=C:WINDOWSSYSTEM
[True Type fonts list]
ARIAL.TTF=64
ARIALBD.TTF=65
ARIALI.TTF=66
ARIALBI.TTF=67
TIMES.TTF=68
TIMESBD.TTF=69
TIMESI.TTF=70
TIMESBI.TTF=71
COUR.TTF=72
COURBD.TTF=73
COURI.TTF=74
COURBI.TTF=75
[Use All True Type fonts]
All=0
The TiniFile class is not a Delphi component, so it cannot be found in Delphi's VCL template. It is defined in the inifiles unit in the Delphi system. Therefore, to use the TiniFile class, you must explicitly use the Uses inifiles directive in the unit file that uses this class. illustrate.
There are many member functions defined in the TiniFile class. Here are a few more frequently used member functions:
⑴Create()
The function is defined as: constructor Create(const FileName: string);
This function creates an object of the TiniFile class. The parameter FileName is the name of the initialization file to be read and written.
If the file to be read and written is in the Windows directory (such as the system.ini file), you can directly write the file name without specifying the path, otherwise you must specify the path (such as d:ucdos dfnt.ini).
If the file exists in the specified directory according to the following rules, open the initialization file; otherwise, create the initialization file in the specified directory.
⑵ReadSections()
The process is defined as: PRocedure ReadSections(Strings: TStrings);
This process will read all node names (that is, the part enclosed in [] brackets, such as [True Type fonts] in the rdfnt.ini file) from the created TiniFile class object (that is, the initialization file associated with it) list]) is stored in the string list. The parameter Strings is the variable name of the string list.
⑶ ReadSectionValues()
The procedure is defined as: procedure ReadSectionValues(const Section: string; Strings: TStrings);
This process converts each keyword (such as ARIALBI.TTF) in the node corresponding to the value of the parameter Section (such as [True Type fonts list] in the rdfnt.ini file) and the value it contains (such as the ARIALBI.TTF keyword value is 67) is read into the string list specified by the parameter Strings.
⑷ ReadSection()
The procedure is defined as: procedure ReadSection(const Section: string; Strings: TStrings);
This process reads each keyword in the node corresponding to the value of the parameter Section into the string list specified by the parameter Strings. The difference with ReadSectionValues() is that it does not read the corresponding value of each keyword.
⑸ ReadString()
The function is defined as: function ReadString(const Section, Ident, Default: string): string;
This function returns the keyword value corresponding to the node name with the value of the parameter Section and the keyword name with the value of the parameter Ident (for example, the value of the ARIALBI.TTF keyword in the [True Type fonts list] section is 67). When the keyword in the specified node or section does not exist, the function returns the default value of the parameter Default. The returned value is a string data.
When the data type of the keyword value in the specified node is not a string, you can use the ReadInteger() member function to read an integer value and the ReadBool() member function to read a Boolean value.
⑹ WriteString()
The procedure is defined as: procedure WriteString(const Section, Ident, Value: string);
This process sets the keyword value in which the value of the parameter Section is the node name and the value of the parameter Ident is the keyword name to the value of the parameter Value. This process sets string data.
When both the specified node and keyword exist, the value of Value is used to replace the original value; if the specified node does not exist, a node is automatically added to the associated initialization file, and the value of the node is the value of the parameter Section, and in the A keyword is automatically added under the node. The keyword name is the value of the parameter Ident, and the value corresponding to the keyword is the value of the parameter Value. If the node exists but the keyword does not exist, a keyword is automatically added under the node. The keyword name is the value of the parameter Ident, and the value corresponding to this keyword is the value of the parameter Value.
To set an integer value, call the WriteInteger() member function; use the WriteBool() member function to set a Boolean value.
Knowing the functions of the above functions, it is not difficult to create or read and write an initialization file. The following uses a practical example to illustrate the reading method of the initialization file. The steps are as follows:
⒈ Place two combined list boxes named SectionComboBox and IdentComboBox on the form that needs to read and write the initialization file. The SectionComboBox stores the node name and the IdentComboBox stores the key name of the selected node. An input box named IdentValueEdit stores the value of the corresponding keyword. The modification command button named CmdChang can be used to modify the value of the keyword. After modification, use the storage command button named CmdSave to save the modified keyword value into the associated initialization file. The unit name corresponding to the form is set to IniUnit, the form name is set to IniForm, and the form layout is as shown in Figure 1 below:
⒉ In the interface part of the IniUnit unit, use uses inifiles; to indicate the unit name defined by the TiniFile class to be referenced. And define the object of TiniFile class in the variable description part, such as
var IniFile: TiniFile;
⒊ Establish the OnCreate event procedure of the form. Use the Create member function of the TIniFile class to create a TIniFile object, use this object to read and write the rdfnt.ini initialization file in the d:ucdos directory, and read all the nodes in the initialization file into the SectionComboBox combination list box through the ReadSections() member function. , use the ReadSection() member function to read all the keywords in the first node into the IdentComboBox In the combined list box, use the ReadString() member function to send the value of the first keyword to the IdentValueEdit input box.
⒋ Create the OnChange event process of the SectionComboBox combined list box. When selecting different items in the list box (that is, different node names), use the ReadSection() member function to read all the keywords in the selected node into the IdentComboBox combined list box, and use the ReadString() member function to read the first The value of the keyword is sent to the IdentValueEdit input box.
⒌ Establish the OnChange event process of the IdentComboBox combination list box. When selecting different items (that is, different keyword names) in the list box, use the ReadString() member function to send the value of the keyword into the IdentValueEdit input box.
⒍ Create the OnClick event process of the command button CmdChang. Make the content in the IdentValueEdit input box modifiable (the IdentValueEdit input box cannot be modified without pressing the command button), and set the command button CmdSave to be valid, so that the modified keyword value can be stored in the associated initialization file.
⒎ Create the OnClick event process of the command button CmdSave. If the keyword value has changed, call the WriteString() member function to save the modified keyword value to disk.
⒏ Create the OnDestroy event procedure of the form. When the form fails, the created TIniFile object is released to release the system resources used by the object.
At this point, after running the project, the reading and writing of the initialization file can proceed smoothly. Of course, you can also use the EraseSection() member function to delete the specified section, and you can also use the DeleteKey() member function to delete the specified keyword. Due to limited space, I will not introduce it in detail here. If you are interested, you can refer to the Delphi usage help.
The following is the source code of this unit:
unit IniUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, inifiles;
type
TIniForm = class(TForm)
SectionComboBox: TComboBox;
Label1: TLabel;
CmdSave: TButton;
CmdChang: TButton;
IdentComboBox: TComboBox;
IdentValueEdit: TEdit;
Label2: TLabel;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure SectionComboBoxChange(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure CmdChangClick(Sender: TObject);
procedure CmdSaveClick(Sender: TObject);
procedure IdentComboBoxChange(Sender: TObject);
private
{Private declarations}
public
{Public declarations}
end;
var
IniForm: TIniForm;
{Read and write Windows initialization files through the TIniFile class in Delphi}
IniFile: TIniFile;
implementation
{$R *.DFM}
procedure TIniForm.FormCreate(Sender: TObject);
begin
{ Use the Create member function of the TIniFile class to create a TIniFile pair
Object, this object is used to read and write the rdfnt.ini file in the d:ucdos directory.
If the file to be read and written is in the Windows directory (such as system.ini),
You can write the file name directly without specifying the path}
IniFile:=TIniFile.Create('d:ucdos dfnt.ini');
{ All the contents in the initialization file system.ini associated with the TIniFile object
Section names with sections (that is, the part enclosed by [] brackets) are sent to the drop-down group
Combo list box SectionComboBox}
SectionComboBox.Clear;
IniFile.ReadSections(SectionComboBox.Items);
{Select the first section name of the system.ini file}
SectionComboBox.ItemIndex:=0;
SectionComboBoxChange(Sender);
CmdSave.Enabled:=False;
end;
{The corresponding items in the selected section in the combo list box IniComboBox
The variables and corresponding values are sent to the multi-line text editor IniMemo}
procedure TIniForm.SectionComboBoxChange(Sender: TObject);
begin
IdentComboBox.Clear;
IniFile.ReadSection(SectionComboBox.Text,
IdentComboBox.Items);
IdentComboBox.ItemIndex:=0;
IdentComboBoxChange(Sender);
end;
procedure TIniForm.IdentComboBoxChange(Sender: TObject);
begin
IdentValueEdit.Enabled:=False;
{Read the selected keyword value}
IdentValueEdit.Text:=
IniFile.ReadString(SectionComboBox.Text,
IdentComboBox.Text,');
end;
procedure TIniForm.CmdChangClick(Sender: TObject);
begin
CmdSave.Enabled:=True;
IdentValueEdit.Enabled:=True;
IdentValueEdit.SetFocus;
end;
procedure TIniForm.CmdSaveClick(Sender: TObject);
begin
if IdentValueEdit.Modified then begin
IniFile.WriteString(SectionComboBox.Text,
IdentComboBox.Text,
IdentValueEdit.Text);
end;
IdentValueEdit.Enabled:=False;
CmdSave.Enabled:=False;
end;
procedure TIniForm.FormDestroy(Sender: TObject);
begin
IniFile.Free; {release the created object}
end;
end.
The above method has passed debugging using Delphi 3.0 under Windows 95.