Delphi TIniFile class operation ini configuration file

Source: Internet
Author: User
Tags integer numbers

Reproduced in Baidu space: http://hi.baidu.com/graspa/blog/item/d17d928b4ec949d0fc1f1007.html/cmtid/ccf49bd4288f080fa08bb74e

I. It is necessary to understand the structure of the INI file:

; Comment

[Section name]

Keyword = Value

...

---- The INI file allows multiple sections, and each section allows multiple keywords. "=" is followed by the value of this keyword.

---- There are three types of values: String, integer, and Boolean. The strings are stored in the INI file without quotation marks. The Boolean true value is represented by 1, and the Boolean false value is represented by 0.

---- The comment starts with a semicolon.

Ii. Definition

---- 1. Add IniFiles in the Uses section of the Interface;

---- 2. Add a line in the Var variable definition section:

Myinifile: Tinifile;

---- Then, you can create, open, read, and write the variable myinifile.

3. Open the INI File

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

--- The above statement will establish a connection between the variable myinifile and the specific file program. ini. Then, you can use the variable myinifile to read and write the value of the keyword in the program. ini file.

---- It is worth noting that if the file name in the brackets does not specify the path, then this Program. the INI file is stored in the Windows directory, and the Program. to store an INI file in the current directory of an application, specify the complete path and file name. The following two statements can complete this function:

Filename: = ExtractFilePath (Paramstr (0) + 'program. ini ';

Myinifile: = Tinifile. Create (filename );

4. Read the value of a keyword

--- For the string, integer, and boolean data types supported by the INI file, the TINIfiles class provides three different object methods to read the value of the keyword in the INI file.

--- Assume that the defined variables vs, vi, and vb are of the string, integer, and boolean types.

Vs: = myinifile. Readstring ('bar name', 'keyword ', default value );

Vi: = myinifile. Readinteger ('bar name', 'keyword ', default value );

Vb: = myinifile. Readbool ('bar name', 'keyword ', default value );

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

5. Write an INI File

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

Myinifile. writestring ('bar name', 'keyword ', variable or string value );

Myinifile. writeinteger ('bar name', 'keyword ', variable or integer value );

Myinifile. writebool ('bar name', 'keyword ', variable or True or False );

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

6. Delete keywords

---- In addition to adding a keyword to the available write method, the Tinifile class also provides an object method for deleting keywords:

Myinifile. DeleteKey ('bar name', 'keyword ');

VII. Section operations

--- Add a writing method for a section. delete a section and use the following object method:

Myinifile. EraseSection ('bar name ');

--- In addition, The Tinifile class also provides three object methods to operate the Section:

--- Myinifile. readsection ('bar name', TStrings variable); you can read all the keyword names in the specified section to a string list variable;

--- Myinifile. readsections (TStrings variable); read all the section names in the INI file to a string list variable.

---- Myinifile. readsectionvalues ('bar name', TStrings variable); all rows (including keywords, =, and values) in the specified section in the INI file can be read to a string list variable.

8. Release

Use the following statement to release myinifile at an appropriate position:

Myinifile. distory;

9. One instance

---- The following uses a simple example () to demonstrate how to create, read, and store INI files. The myini. ini file contains three keywords: "program parameters", user name (string), formal user (Boolean value), and running time (integer value. The program reads the data in the form creation and writes the myini. ini file when the form is released.

--- Appendix: source program list

Unit Unit1;

Interface

Uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Inifiles, // configuration operation File

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 ', 'running time', 0 ));

Checkbox1.Checked: = myinifile. readbool ('program parameter', 'official user', False );

End;

Procedure TForm1.FormDestroy (Sender: TObject );

Begin

Myinifile. writestring ('program parameter', 'user name', edit1.Text );

Myinifile. writeinteger ('program parameters', 'runtime time', strtoint (edit2.text ));

Myinifile. writebool ('program parameter', 'official user', checkbox1.Checked );

Myinifile. Destroy;

End;

Procedure TForm1.Timer1Timer (Sender: TObject );

Begin

Edit2.Text: = inttostr (strtoint (edit2.text) + 1 );

End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.