Delphi in the initialization. Ini file reading

zhaozj2021-02-08  247

Delphi in the initialization. Ini file reading

Jinlin Hui Guo Rahong

The application in Windows is mostly its own initialization file, such as PowerBuilder, Office, and Cstar. Therefore, the read and write of initialization documents is the technology that each advanced programmer must master. Although the read and write of the initialization file can also be read or written in the text file in Object Pascal, but because the initialization file is different from the general text file, it has its own fixed format (see the following initialization file is the Rdfnt.ini file provided in UCDOS) If reading and writing in a text file, not only the format conversion is very cumbersome, and it is easy to have an error. In order to facilitate the data in the initialization file, the Delphi provides a TiniFile class, which can be used through the TiniFile class. Read and write the initialization file very conveniently.

The content of the RDFNT.INI file in UCDOS is:

[TRUE TYPE FONTS DIRECTORY]

DIR = C: / Windows / System

[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 the Delphi's VCL template, which is defined in the InIfiles unit in the Delphi system, so use the TiniFile class, you must use the USES INIFILES instruction in the unit file using this class. Description.

Many member functions are defined in the TiniFile class, here is a member function with a higher frequency of frequencies:

(1) CREATE ()

The function is defined as: constructor create (const filename: string);

This function creates an object of TiniFile class. The parameter filename is the initialization file name to read and write.

If you read and write in the Windows directory (such as the System.ini file), you can write the file name directly without having to specify the path, otherwise the path (such as D: /ucdos/rdfnt.ini) must be specified.

This initialization file is turned on if the file is existed in the specified directory by following the rules. Otherwise, the initialization file is created in the specified directory.

(2) readsections ()

The process is defined as: Procedure Readsections (Strings: Tstrings);

This process will read all node names from the object of the established TiniFile class (ie, in the initialization file associated with it), such as [True Type Fonts "in the RDFnt.ini file [TRUE TYPE FONTS List]) Deposit into the list of strings. The parameter strings is the variable name of the string list.

(3) ReadsectionValues ​​()

The process is defined as: Procedure ReadsectionValues ​​(const section: string; strings: tstrings);

This process The nodes corresponding to the value of the parameter section (such as the value of each of the RDFNT.INI files (such as arialbi.ttf), such as Arialbi.ttf keywords A value of 67) Read in the string list indicated by the parameter strings. ⑷ ⑷ readsection ()

The process is defined as: Procedure Readsection (Const Section: String; Strings: Tstrings);

This process reads the character string list of each keyword in the node corresponding to the value of the parameter section. Different from 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 value of the parameter section as the node name, the value of the parameter IDENT is a keyword value corresponding to the keyword name (such as the arialbi.ttf keyword of [True Type Fonts List]). When the keyword within the specified node or in the 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 read an integer value with the readinteger () member function, and read a Boolean value with the ReadBool () member function.

⑹ WritestRing ()

The process is defined as: Procedure WriteString (const section, ident, value: string);

The process sets the value of the value of the parameter section as the node name, and the value of the parameter IDT is set to the value of the keyword value to the value of the parameter value. The process is set to string data.

When the specified node and the keyword exist, use the value of Value to replace the original value; if the specified node does not exist, then a node is automatically added in the associated initialization file, the value of the node is the value of the parameter section, and at this The node automatically adds a keyword, the keyword name is the value of the parameter ident, the value corresponding to the parameter value; if the node exists, the keyword does not exist, then a keyword is automatically added under this node. The keyword name is the value of the parameter ident, which corresponds to the value of the parameter value.

To set an integer value, call the WriteInteger () member function; set the Boolean value with the WriteBool () member function.

I know the role of the above functions, it is not difficult to build or read and write a initialization file. The read method of the initialization file is described below with an actual example, steps below:

Bamboo

Forms that need to read and write initialization files (

FORM

Place name

SectionComboBox

,

IdentComboBox

Two combined list boxes, where

SectionComboBox

Store the node name,

IdentComboBox

Store keyword names for the selected node. A name

IdentValueEdit

The input box stores the value of the corresponding keyword. Named

Cmdchang

The modification command button can be used to modify the value of the keyword, and the modified name is used.

Cmdsave

The storage command button stores the value of the keyword of the key to the associated initialization file. The form corresponding to the form is set to

Iniunit

The form name is set

InIFORM

The form layout is shown below:

⒉ The interfab section of the Iniunit unit uses Uses inifiles; describes the unit name defined by the TiniFile class to reference. And in the variable instruction part define the object of the TiniFile class, such as VAR INIFILE: TINIFILE;

⒊ Establish a form of an oncreate event process. Create a TiniFile object using the TiniFile class, read and write the Rdfnt.ini initialization file in the D: / UCDOS directory in this object, and read all nodes in the initialization file through the readsections () member function list in SectionComboBox group list In the box, use the readsection () member function to read all keywords in the first node into the IdentComboBox group list box, and send the value of the first keyword into the IdentValueEdit input box with the ReadString () member function.

⒋ Establish the ONChange event process of the SectionComboX group box. When the different items (ie, different node names) in the selection list box, read all keywords in the selection node into the IdentComboBox group list box with the readSection () member function, and use the readstring () member function will be the first The value of the keyword is sent to the IdentValueEdit input box.

⒌ Establish an onchange event process for the IdentComboBox combination list box. When the different items (ie, different keyword names) in the selection list box, send the value of the keyword to the IdentValueEdit input box with the ReadString () member function.

⒍ Establish the ONCLICK event process of the command button CMDCHANG. Make the contents in the IdentValueEdit input box (not pressing this command, the IdentValueEdit input box cannot be modified) and set the command button cmdsave is valid, you can deposit the modified keyword value to the associated initialization file.

⒎ Establish the onclick event process of the command button CMDsave. If the keyword value has changed, call the WritEString () member function will change the value of the keyword after the keyword.

⒏ Establish the ONDESTROY event process of the form. When the form is invalid, the established TiniFile object is released to release the system resources for the object.

At this point, the operation of the project is running, and the read and write of the initialization file can be smooth. Of course, you can also use the eraseSecion () member function to delete the specified section, or you can use the deletekey () member function to delete the specified keyword, which is limited in detail because of the limited space, and interested in the use of Delphi.

Below is the source code code of the 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;

{DELPHI reads and writes the initialization file of Windows through the TiniFile class}

InIfile: tinifile;

IMPLEMENTATION

{$ R * .dfm}

Procedure TiniForm.FormCreate (Sender: TOBJECT);

Begin

{Create member functions of TiniFile class to establish TiniFile pair

Icon, the object is used to read the rdfnt.ini file in the D: / UCDOS directory,

If you read or not, in the Windows directory (such as system.ini),

Then you can write the file name directly without having to specify the path}

InIfile: = TiniFile.create ('d: /ucdos/rdfnt.ini');

{The initialization file associated with the TiniFile object system.ini

Seasonal name (i.e., the part of [] brackets enclosed) is sent to the drop-down group

TransferComboBox in Section Box

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;

{Each of the corresponding sections in INICOMBOBOX in the combination list box

Variables and corresponding values ​​are sent to 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.WritString (SectionComboBox.text,

IdentComboBox.text,

IdentValueEdit.text);

END;

IdentValueEdit.enabled: = false;

Cmdsave.enabled: = false;

Procedure TiniForm.FormDestroy (Sender: TOBJECT);

Begin

InIfile.Free; {Release Created Object}

END;

End.

Above

WINDOWS 95

Use

Delphi 3.0

Debug pass.

转载请注明原文地址:https://www.9cbs.com/read-1640.html

New Post(0)