Establish, read, store Ini files "one"

zhaozj2021-02-16  56

Content: INI files have important role in system configuration and application parameters, which is important, so visual programming family, such as VB, VC, VFP, Delphi, etc. provide methods of reading and writing INI files, where Delphi is Operation INI files, the most concise, because Delphi6.0 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:; Note [small name] keyword = value ...

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. Note Taking a semicolon ";" starting.

Second, define 1. Infiles; 2, add a line in the VAR variable definition section: Myinifile: Tinifile; then, you can create, open, read, read, write, write, etc.

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 accomplish this feature: filename: = extractFilePath (paramstr (0)) 'program.ini'; myinifile: = tinifile.create (filename);

Fourth, read the value of the keyword

For three data types supported by INI files, integer values, Boolean data types, TiniFiles classes provide three different object methods to read the value of keywords in the INI file.

Suppose the variables Vs, VI, VB have been defined, respectively, Integer, and Boolean types. vs: = myinifile.readstring ('small name ",' keyword ', default); vi: = myinifile.readinteger (' small name", 'keyword', default); VB: = Myinifile.ReadBool ('Small name', 'keyword', default);

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

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.RaseSection ('small name');

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 small section in the INI file Take it to a string list variable.

MyiniFile.ReadsectionValues ​​('small name ", TStrings variable); all rows (including keywords, =, values) that specify in the INI file can be read to a string list variable.

Eight, release

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

MyiniFile.Distory;

Nine, one example

Below with a simple example (Figure), a method of establishing, reading, and storage INI files is demonstrated. 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.

Advanced program list

Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, InIfiles, Stdctrls, ExtCtrls

type TForm1 = class (TForm) Edit1: TEdit; Edit2: TEdit; CheckBox1: TCheckBox; Timer1: TTimer; procedure FormCreate (Sender: TObject); procedure FormDestroy (Sender: TObject); procedure Timer1Timer (Sender: TObject); private {Private Declarations}}}} {public declarations}

Var Form1: TFORM1; MyiniFile: Tinifile; Implementation

{$ R * .dfm}

procedure TForm1.FormCreate (Sender: TObject); varfilename: string; beginfilename: = ExtractFilePath (paramstr (0)) 'myini.ini'; myinifile: = TInifile.Create (filename); edit1.Text: = myinifile.readstring ( 'Program parameters', 'User Name', 'Default User Name'); Edit2.Text: = INTOSTR (MyiniFile.Readinteger ('program parameter', 'has run time', 0); checkbox1.checked: = MyiniFile.ReadBool ('program parameter', 'is formal user', false);

Procedure TForm1.FormDestroy (Sender: Tobject); BeginmyiniFile.WritString ('program parameter', 'user name ", edit1.text); MyiniFile.WriteInteger (' program parameter ',' has run time ', start (edit2.text) ); myinifile.writebool ( 'program parameters', 'is the formal user', checkbox1.Checked); myinifile.Destroy; end; procedure TForm1.Timer1Timer (Sender: TObject); beginedit2.Text: = inttostr (strtoint (edit2.text ) 1);

This instance is debugged under Delphi6.0 WinXP.

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

New Post(0)