INI File Management in Delphi

Source: Internet
Author: User
Tags integer numbers
INIFILE Management

By clin003 at 20070528 from: http://blog.csdn.net/clin003
INI files play an important role in system configuration and saving and setting application parameters, for example, VB, Vc, VFP, and Delphi all provide methods to read and write INI files. Among them, operations on INI files in Delphi are the most concise, Because delphi provides a Tinifile class, this allows us to process INI files flexibly.

I. ini file structure:
; Comment
[Section name]
Keyword = Value
...
The INI file can have multiple sections, and each section can have 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. Define keywords
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 a path, the program. ini file will be stored in the Windows directory.

You can store the program. ini file in the current application directory by specifying 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
The tinifiles class provides three different object methods to read the value of keywords in the INI file for the string, integer value, and boolean data types supported by 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 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 );
If this INI file does not exist, the preceding 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
You can add a writing method for a section to delete a section. The following object methods are available:
Myinifile. erasesection ('bar name ');

In addition, the Tinifile class also provides three object methods to operate on the section:
Myinifile. readsection ('bar name' tstrings variable); all the keyword names in the specified section can be read 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;

Demo 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.

Source program

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;
Procedureformcreate (Sender: tobject );
Procedureformdestroy (Sender: tobject );
Proceduretimer1timer (Sender: tobject );

Private
{Privatedeclarations}
Public
{Publicdeclarations}
End;

VaR
Form1: tform1;

Implementation
VaR
Myinifile: Tinifile;
 
{$ R *. DFM}
By clin003 at 20070528 from: http://blog.csdn.net/clin003
Proceduretform1.formcreate (Sender: tobject );
VaR
Filename: string;
Begin
Filename: = extractfilepath (paramstr (0) + 'myini. ini ';
Myinifile: = Tinifile. Create (filename); // open the INI File
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;

Proceduretform1.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;

Proceduretform1.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.