Delphi reads the INI file

zhaozj2021-02-16  54

Ini files have important roles in system configuration and application parameters, which is important, so visualized programming, such as VB, VC, VFP, Delphi, etc., provide methods of reading and writing Ini files, where Delphi is operating ini File, the most concise, this is because Delphi provides a TiniFile class so that we can deal with the INI file very flexible.

One. INI file structure

[Small name] INI file

Keyword 1 = value 1

Key sub 2 = value 2

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

There are three types of values: strings, integer values, and Boolean values. 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.

Second, definition

1. Add inIfiles in the Uses section of Interface;

2, add a line in the definition part of the VAR variable: MyiniFile: Tinifile;

Define an instance of the class. Then, the variable MyiniFile can be created, opened, read, written, and other operations.

Third, open the INI file

MyiniFile: = TiniFile.create (Program.ini);

The above row statement will be connected to the variable MyiniFile and the specific file Program.ini, and then you can read the keywords in the programm.ini file by variable myinifile.

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

Filename: = ExtractFilePath (paramstr

(0)) program.ini;

Myinifile: = TiniFile.create (filename);

V. Write ini file

Similarly, the TiniFile class also provides three different object methods to write strings, integer and Boolean keywords to the INI file.

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 (small name, keyword);

Seven, section operation

Increases a method of using a write method to complete, delete a section available below:

MyiniFile.eraseSection;

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

MyiniFile.Readsection (small name, tstrings variable); you can 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); read all rows (including keywords, =, values) of the section in the INI file to a string list variable.

Eight, release

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

The following is a specific example. The source code is as follows. Created a Myini.ini file, there is a section called Newini, there are 3 keyword "user name" "Running time" "Official users". Running effect, you can fill in "User Name" in Edit1; edit2 display time, can not change the value; checkbox1 enters the Myini.ini file by taking the hook, save time, and user name "to the Myini.ini file, displayed The time saved and filled in the "User Name", if you modify, the effect, and the modification during the Myini.ini file is modified during the program run.

Unit unit1;

Interface

Uses

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;

IMPLEMentation

VAR

MyiniFile: tinifile;

{Unique instances of a class}

{$ R * .dfm}

Procedure TFORM1.FormCreate (Sender: TOBJECT);

VAR

FILENAME: STRING;

Begin

{The writing form of the following two lines, create ini files 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', 'has run time', 0);

Checkbox1.checked: = myinifile.readbool

('newini', 'Is it official user', false;

{newini is the nickname, the intermediate field is the name of the keyword, and the third field is the default. When myini.ini does not exist, the above statement automatically creates this file, the quotation marks in the top, is single quotes}

END;

Procedure TFORM1.FORMDESTROY (Sender: TOBJECT);

Begin

MyiniFile.WritString ('newini', 'user name "; edit1.text);

MyiniFile.Writeinteger ('newini', 'has run time',

StrtOINT (Edit2.Text));

MyiniFile.WriteBool ('newini', 'is officially user', checkbox1.checked);

MyiniFile.Destroy;

END;

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

Begin

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

END;

End.

INI file

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

New Post(0)