Delphi INI file read/write

Source: Internet
Author: User
Tags inif

Reference: http://www.cnblogs.com/zhangzhifeng/archive/2011/12/01/2270267.html

I. Structure of INI file

This is a comment about the INI file [node] keyword = value ...

INI file allows multiple nodes, each with multiple keywords allowed, and "=" followed by the value of the keyword (there are three types: string, Integer, and Boolean.) Where the string is stored in the INI file without quotation marks, Boolean truth value is expressed in 1, Boolean false values are represented by 0. Comment begins with a semicolon ";".

Ii. operation of INI file

1. Added Inifiles in interface's uses festival

2. Add a line to the var variable definition section:

Inifile:tinifile;

The variable inifile can then be created, opened, read, written, and so on.

3. Open the INI file:

inifile:= tinifile.create (' Tmp.ini '); the//create parameter is a string of the path to the INI file

4. Read the value of the keyword:

A:= Inifile.  Readstring (' node ', ' keyword ', default value); String Type b:= inifile.  Readinteger (' node ', ' keyword ', default value); The integer type c:= inifile.  Readbool (' node ', ' keyword ', default value); Boolean type

where [default] is the default value returned if the INI file does not exist for this keyword

5. Write INI file:

Infile.writestring (' node ', ' keyword ', variable or string value);    Writes the string type infile.writestring (' node ', ' keyword ', variable or integer value);    Writes an integer type infile.writestring (' node ', ' keyword ', variable or TRUE or false);    Write Boolean type

When the node for this INI file does not exist, the above statement will also automatically create the INI file

6. Delete Keywords:

InFile.  DeleteKey (' node ', ' keywords '); Delete the keyword inifile.  Erasesection (' node '); Delete a node

  

7. Node Operation:

Inifile.readsection (' node ', tstrings variable);  All key names of the specified node can be read into a string list variable inifile.readsections (tstrings variable); All the nodes in the INI file can be read into a string list variable.  Inifile.readsectionvalues (' node ', tstrings variable); All rows of the specified node in the INI file (including keywords, =, values) can be read into a string variable

  

8. Release:  

Inifile.destroy;

Or

Inifile.free;

  

Iii. examples

Unit unit1;interfaceuses  Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,  Dialogs, Inifiles, stdctrls;type  TForm1 = Class (Tform)    Button1:tbutton;    Button2:tbutton;    Procedure Formcreate (sender:tobject);    Procedure Button1Click (sender:tobject);    Procedure Button2click (sender:tobject);    Procedure Formdestroy (sender:tobject);  Private    {Private declarations}  public    {public declarations}    Inif:tinifile;  End;var  form1:tform1;implementation{$R *.dfm}procedure tform1.formcreate (sender:tobject); Begin  Inif:= Tinifile.create (Extractfilepath (paramstr (0)) + ' T.ini '); end;procedure Tform1.button1click (sender:tobject); begin  inif.writestring (' T ', ' t1 ', ' info ');  Modify End;procedure Tform1.button2click (sender:tobject); Begin  Inif.deletekey (' T ', ' T2 ');   Delete End;procedure Tform1.formdestroy (sender:tobject); begin  Inif.free;end;end.

  

Delphi INI file read/write

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.