Using Delphi to operate the INI File

Source: Internet
Author: User

Delphi provides a Tinifile class that allows us to flexibly process INI files.


I. Structure of the INI File
[Section name] INI File
Keyword 1 = value 1
Key child 2 = value 2
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.

 

Ii. Definition

1. added in the uses section of the interfaceInifiles;
2. Add a line in the VaR variable definition section:Myinifile: Tinifile;
Defines an instance of the class. 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 INI File

Myinifile. readstring (segment name, keyword, variable or string value); // read the string

Myinifile. readinteger (segment name, keyword, variable or integer value); // read integer

Myinifile. readbool (segment name, keyword, variable or true or false); // read Boolean Type

 

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 (segment name, keyword, variable or string value );
Myinifile. writeinteger (segment name, keyword, variable or integer value );
Myinifile. writebool (section 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 (section name );
In addition, the Tinifile class also provides three object methods to operate on the section:
Myinifile. readsection (section 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 (section name, tstrings variable); all rows (including keywords, =, and values) of 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;
The following is an example. The source code is as follows. A myini. ini file named newini is created, with three keywords: "User Name", "running time", and "official user ". Run effect. You can enter "User Name" in edit1; edit2 shows the time, and the value cannot be changed. checkbox1 checks the time and user name to enter myini. in the INI file, when the application is re-opened, the saved time and the entered "User Name" are displayed. the INI file is modified in the same way as when the program is running.

 

 


 



 

Related Article

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.