INI file programming in Delphi

Source: Internet
Author: User
Tags integer numbers


---- INI files play an important role in system configuration and saving and setting application parameters. Therefore, a Visual Programmer, 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 delphi3 provides a Tinifile class, this allows us to process INI files flexibly.

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

2. 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, 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.