Detailed explanation of INI File Operations in Delphi

Source: Internet
Author: User

NI files play an important role in system configuration and saving and setting application parameters. Therefore, Visual Programming, for example, VB, VC, VFP, and Delphi all provide methods for reading and writing 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. It is necessary to understand the structure of the INI file:; 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. 2. Define 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 be the variable myinifile and the specific file program. you can use the myinifile variable to read and write program. the value of the keyword in the 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. The value of the read keyword is applicable to 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 INI file does not exist. 5. 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); when the INI file does not exist, the above statement will automatically create the INI file. 6. In addition to adding a keyword to the available write method, the Tinifile class also provides an object method for deleting the Keyword: myinifile. deleteKey ('bar name', 'keyword '); 7. Add a writing method for the Section in the Section operation. 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 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 the names of all sections 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 myinifile: myinifile. distory;

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.