INI file programming

xiaoxiao2021-03-14  196

INI file programming in delphi

Squiring Editor: China ASP

---- INI files have important roles in system configuration and application parameters, so it is a visual programming family, such as VB, VC, VFP, Delphi, etc. provide methods of reading and writing Ini files, of which Delphi Operates the INI file, which is the most simple, because Delphi3 provides a TiniFile class that allows us to handle the INI file very flexible.

First, it is necessary to understand the structure of the INI file:

Notes

[Small name]

Keyword = value

...

---- INI file allows multiple sub-sections, and each section is allowed to have multiple keywords, "=" later is the value of the keyword.

---- Value type has three types: string, integer value, and Boolean value. There is no quotation mark when the string is stored in the INI file, and the Boolean true value is represented by 1, and the Boolean values ​​are represented by 0.

---- Note Taking a semicolon ";" starting.

Second, definition

---- 1, add inIfiles in the Uses section of Interface;

---- 2, increase the line in the definition part of the VAR variable:

MyiniFile: tinifile;

---- Then, the variable MyiniFile can be created, opened, read, written, etc.

Third, open the INI file

Myinifile: = TiniFile.create ('program.ini');

--- This line of statements will be connected to the variable MyiniFile and the specific file Program.ini, and then you can read the keywords in the Program.ini file by variable myiniFile.

---- It is worth noting that if the file name in parentheses does not specify the path, then this Program.ini file is stored in the Windows directory, and the method stored in the current directory of the program in the application of the Program.ini file is: It specifies a complete path and file name. The following two statements can complete this feature:

FileName: = ExtractFilePath (paramstr (0)) 'Program.ini';

Myinifile: = TiniFile.create (filename);

Fourth, read the value of the keyword

--- For the string of INI files, the integer value, the Boolean data type, the TiniFiles class provides three different object methods to read the value of the keywords in the INI file.

--- Suppose the variables Vs, VI, VB have been defined, and the INTEGER, the Boolean type is defined.

VS: = MyiniFile.Readstring ('small name ",' keyword ', default);

VI: = MyiniFile.Readinteger ('small name ",' keyword ', default);

VB: = MyiniFile.ReadBool ('Name ",' Keyword ', default);

--- The default value is the default value returned when the INI file does not exist.

V. Write ini file

---- Similarly, TiniFile class also provides three different object methods, writing to the INI file, is a keyword of the string, integer and Boolean type.

MyiniFile.WritString ('small name ",' keyword ', variable or string value);

MyiniFile.Writeinteger ('small name ",' keyword ', variable or integer value);

MyiniFile.WriteBool ('small name ",' keyword ', variable or true or false;

---- When this INI file does not exist, the above statement will automatically create the INI file.

Six, delete keywords

---- In addition to adding a keyword, the TiniFile class also provides an object method for deleting keywords: MyiniFile.DeleteKey;

Seven, section operation

--- Increase a method of using a written method to complete, delete a subtooth available to the following object:

MyiniFile.RaseSection ('small name');

--- In addition, the TiniFile class also provides three object methods to operate on the section:

--- MyiniFile.Readsection ('small name ", tstrings variable); read all keyword names in the specified section to a string list variable;

--- MyiniFile.Readsections (TSTRINGS variable); you can read all the subordenses in the INI file to a string list variable.

---- MyiniFile.ReadsectionValues ​​('small name ", tstrings variable); all rows (including keywords, =, values) of the specified section (including keywords, =, values) can be read in a string list variable in the INI file.

Eight, release

In the appropriate location, use the following statement to release MyiniFile:

MyiniFile.Distory;

Nine, one example

---- The following is a simple example (as shown), demonstrating the method of establishing, reading, and storage INI files. The Myini.ini file contains three keywords with the "Program Parameters" section, and the user name (string), whether formal user (Boolean) and the run time (integer). The program is established in the form to read the data and write myini.ini files when the form is released.

--- A list of attachment procedures

Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, INIFILES, STDCTRLS, EXTCTRLS

Type

TFORM1 = Class (TFORM)

EDIT1: TEDIT;

Checkbox1: Tcheckbox;

EDIT2: TEDIT;

Label1: TLABEL;

Label2: TLABEL;

Timer1: TTIMER;

Label3: TLABEL;

Procedure formcreate (Sender: TOBJECT);

Procedure FormDestroy (Sender: TOBJECT);

Procedure Timer1Timer (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMENTATION

VAR

MyiniFile: tinifile;

{$ R * .dfm}

Procedure TFORM1.FormCreate (Sender: TOBJECT);

VAR

FILENAME: STRING;

Begin

Filename: = ExtractFilePath (paramstr (0)) 'Myini.ini';

Myinifile: = TiniFile.create (filename);

Edit1.text: = myinifile.readstring ('program parameter', 'user name ",' default user name ');

Edit2.Text: = INTTOSTR (MyiniFile.Readinteger ('program parameter', 'has run time', 0));

Checkbox1.checked: = myinifile.readbool ('program parameter', 'is formal user', false);

Procedure TFORM1.FORMDESTROY (Sender: TOBJECT);

Begin

MyiniFile.writestring ('program parameter', 'user name "; edit1.text);

MyiniFile.WriteInteger ('program parameter', 'has run time', strtoint (edit2.text));

MyiniFile.writeBool ('program parameter', 'is formal user', checkbox1.checked;

MyiniFile.Destroy;

END;

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

Begin

Edit2.Text: = INTTOSTR (strtOINT (Edit2.Text) 1);

END;

End.

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

New Post(0)