Delphi method of establishing, reading and storing INI file "one" _delphi

Source: Internet
Author: User
Tags integer numbers
First, it is necessary to understand the structure of the INI file:
; note
[section name]
Keyword = value
...
The INI file allows multiple subsections, each of which allows multiple keywords, followed by the value of the keyword.
There are three types of values: strings, integer values, and Boolean values. When the string is stored in the INI file, there is no quotation mark, the Boolean truth is 1, and the Boolean false value is 0.
Comment with semicolon ";" Beginning.
Ii. definition
1. Increase the inifiles in the uses section of interface;
2. Add one line to the definition section of var variable:
Myinifile:tinifile;
The variable myinifile can then be created, opened, read, written, and so on.
Three, open the INI file
Myinifile:=tinifile.create (' Program.ini ');
This line of statements will link the variable myinifile to the specific file Program.ini, and then you can read and write the value of the keyword in the Program.ini file by using the variable myinifile.
Notably, if the filename in parentheses does not indicate the path, 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 work
Yes:
Filename:=extractfilepath (paramstr (0)) + ' Program.ini ';
Myinifile:=tinifile.create (filename);
Read the value of the keyword
The Tinifiles class provides three different object methods to read the values of the keys in the INI file, for the string, Integer, and Boolean three data types supported by the INI file.
Assume that the defined variables vs, vi, and VB are string, Integer, and Boolean, respectively.
Vs:=myinifile. Readstring (' section name ', ' keyword ', default value);
Vi:=myinifile. Readinteger (' section name ', ' keyword ', default value);
Vb:=myinifile. Readbool (' section name ', ' keyword ', default value);
The default value is the default value that is returned when the INI file does not exist for that keyword.
V. Write INI file
Similarly, the Tinifile class also provides three different object methods, writing strings, integer numbers, and boolean-type keywords to the INI file.
Myinifile.writestring (' section name ', ' keyword ', variable or string value);
Myinifile.writeinteger (' section name ', ' keyword ', variable or integer value);
Myinifile.writebool (' section name ', ' keyword ', variable or TRUE or false);
When this INI file does not exist, the above statement will also automatically create the INI file.
Six, delete the key word
In addition to adding a keyword to the available write methods, the Tinifile class also provides an object method for deleting a keyword:
Myinifile. DeleteKey (' section name ', ' keyword ');
Seven, section operation
Add a section that can be written to complete, and delete a section to use the following object method:
Myinifile. Erasesection (' section name ');
In addition, the Tinifile class also provides three object methods to manipulate the subsections:
Myinifile.readsection (' subsection name ', tstrings variable); You can read all the keyword names in the specified section to a string list variable;
Myinifile.readsections (tstrings variable); You can read all the section names in the INI file to a string list variable.
Myinifile.readsectionvalues (' section name ', tstrings variable); You can read all the lines in the INI file, including keywords, =, values, into a string list variable.
Viii. Release
Release the myinifile in the appropriate place with the following statement:
Myinifile.distory;
九、一个 instance
The following is a simple example (pictured) that demonstrates how to create, read, and store INI files. The Myini.ini file contains the "Program Parameters" section, and the user name (string), whether the official user (Boolean value) and elapsed time (integer value) of three keywords. The program builds the data on the form and writes the Myini.ini file when the form is released.
List of source-attached programs
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs,inifiles, Stdctrls, Extctrls;
Type
TForm1 = Class (Tform)
Edit1:tedit;
Edit2:tedit;
Checkbox1:tcheckbox;
Timer1:ttimer;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Timer1timer (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Myinifile:tinifile;
Implementation
{$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 parameters ´,´ user name ´,´ Default user name ´);
edit2.text:= IntToStr (Myinifile.readinteger (´ program parameter ´,´ elapsed time ´,0));
CheckBox1. checked:= myinifile.readbool (´ program Parameters ´,´ whether the official user ´,false);
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Myinifile.writestring (´ program parameter ´,´ user name ´,edit1. Text);
Myinifile.writeinteger (´ program parameter ´,´ elapsed time ´,strtoint (Edit2.text));
Myinifile.writebool (´ program parameter ´,´ whether the official user ´,checkbox1. Checked);
Myinifile. Destroy;
End
Procedure Tform1.timer1timer (Sender:tobject);
Begin
Edit2. Text:=inttostr (Strtoint (Edit2.text) +1);
End
End.

This instance is debugged under DELPHI6.0+WINXP.

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.