Delphi Operation INI File

Source: Internet
Author: User
Tags open ini file

Delphi provides a Tinifile class that allows us to handle INI files with great flexibility


A INI file structure
[Section name]ini file
Keyword 1= value 1
Key sub-2= value 2
INI file allows multiple subsections, each of which allows for multiple keywords, followed by "=" with the value of the keyword.
There are three types of values: String, Integer value, and Boolean value. Where the string is stored in the INI file without quotation marks, Boolean truth value is denoted by 1, Boolean false values are expressed in 0.

Second, the definition

1, the uses festival in interface increased inifiles;
2, add a line in the var variable definition section: myinifile:tinifile;
Defines an instance of a class. The variable myinifile can then be created, opened, read, written, and so on.

Third, open INI file
Myinifile:=tinifile.create (Program.ini);
This line of statements will establish a link between the variable myinifile and the specific file Program.ini, and then you can read and write the values of the keywords in the Program.ini file through the variable myinifile.
It is worth noting that if the file name in parentheses does not indicate a path, then the Program.ini file is stored in the Windows directory, and the Program.ini file is stored in the application's current directory by specifying the full path and file name. The following two statements can complete this function:
Filename:=extractfilepath (paramstr (0)) +program.ini;
Myinifile:=tinifile.create (filename);

Iv. reading INI file

Myinifile.  Readstring (subsection name, keyword, variable or string value); Read string

Myinifile.  Readinteger (subsection name, keyword, variable or integer value); Read integer

Myinifile.readbool (subsection name, keyword, variable or true or false); Read Boolean type

V. Write INI file
Similarly, the Tinifile class provides three different object methods, writing strings, integers, and Boolean types to the INI file.

Myinifile.writestring (subsection name, keyword, variable or string value);
Myinifile.writeinteger (subsection name, keyword, variable or integer value);
Myinifile.writebool (subsection name, keyword, variable or true or false);
When this INI file does not exist, the above statement will also automatically create the INI file

Vi. deletion of Keywords
In addition to using the Write method to add a keyword, the Tinifile class also provides an object method for deleting the keyword:

Myinifile. DeleteKey (subsection name, keywords);

Seven, subsection operation
Adding a section can be done by writing the method, deleting a section using the following object method:

Myinifile. Erasesection (subsection name);
In addition, the Tinifile class provides three object methods to manipulate the subsections:
Myinifile.readsection (subsection name, tstrings variable); All key names in the specified section can be read into a string list variable;
Myinifile.readsections (tstrings variable); All the section names in the INI file can be read into a string list variable.
Myinifile.readsectionvalues (subsection name, tstrings variable); All rows (including keywords, =, values) in the specified section of the INI file can be read into a string list variable.

Viii. Release
In the appropriate location, release myinifile with the following statement:
Myinifile.distory;
Here is a concrete example. The source code is as follows. A Myini.ini file was created with a section named Newini with 3 keywords "user name" "Elapsed Time" or "official user". Operation effect, you can fill in the Edit1 "user name", edit2 display time, can not change the value, CheckBox1 by tick, save time and "user name" into the Myini.ini file inside, reopen the application, displayed when the time saved and fill in the " User name ", if modified inside the Myini.ini file, the effect is the same as when the program is running.

Delphi Operation INI File

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.