INI File Operation Note (1): Use Tinifile

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: tbutton; button7: tbutton; Procedure destroy (Sender: tobject); Procedure formdestroy (Sender: tobject ); procedure extract (Sender: tobject); Procedure button2click (Sender: tobject); Procedure extract (Sender: tobject ); procedure button6click (Sender: tobject); Procedure button7click (Sender: tobject); Private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} uses inifiles; {uses contains Tinifile units} var ini: Tinifile; Path: string; {INI file path} section, key: string; {represents the sections and Keywords of the INI file} {INI file structure:; Comment [section name] keyword = value: INI file support: the true and false values of string, integer, Boolean, date, time, datetime, double, and binary string values without quotation marks are represented by 1 and 0.} procedure tform1.formcreate (Sender: tobject); begin path: = changefileext (paramstr (0 ),'. INI '); ini: = Tinifile. create (PATH); {ini object creation requires file path parameters. If the path is missing, the default Windows Directory will be} end; // write the INI file: Procedure tform1.button1click (Sender: tobject ); begin section: = 'aaa'; key: = 'astring'; ini. writestring (section, key, 'aaa-string'); key: = 'ainteger '; ini. writeinteger (section, key, 111); key: = 'Lean lean '; ini. writebool (section, key, true); key: = 'adate'; ini. writedate (section, key, now); key: = 'atime '; ini. writetime (section, key, now); key: = 'adatetime'; ini. writedatetime (section, key, now); key: = 'adouble'; ini. writefloat (section, key, Pi); Section: = 'bbb '; key: = 'bstring'; ini. writestring (section, key, 'bbb-string'); key: = 'binteger '; ini. writeinteger (section, key, 222); key: = 'bboolean'; ini. writebool (section, key, true); key: = 'bdate'; ini. writedate (section, key, now); key: = 'btime'; ini. writetime (section, key, now); key: = 'bdatetime'; ini. writedatetime (section, key, now); key: = 'bdouble'; ini. writefloat (section, key, Pi); Section: = 'ccc '; key: = 'cstring'; ini. writestring (section, key, 'ccc-string'); key: = 'cinteger'; ini. writeinteger (section, key, 333); key: = 'cboolean'; ini. writebool (section, key, false); key: = 'date'; ini. writedate (section, key, now); key: = 'ctime'; ini. writetime (section, key, now); key: = 'cdatetime'; ini. writedatetime (section, key, now); key: = 'cdouble'; ini. writefloat (section, key, Pi); {Write result: [AAA] astring = aaa-string ainteger = 111 bytes lean = 1 adate = 2007-12-17 atime = 22:06:23 adatetime = 2007-12-17 22:06:23 Adouble = 3.14159265358979 [BBB] bstring = BBB-string binteger = 222 bboolean = 1 bdate = 2007-12-17 btime = 22:06:23 bdatetime = 2007-12-17 22:06:23 bdouble = 3.14159265358979 [CCC] cstring = CCC-string cinteger = 333 cboolean = 0 cdate = 2007-12-17 ctime = 22:06:23 cdatetime = 2007-12-17 22:06:23 cdouble = 3.14159265358979} end; // read the INI file: Procedure tform1.button2click (Sender: tobject); var S: string; I: integer; B: Boolean; F: Double; D: tdate; T: tTime; DT: tdatetime; begin S: = ini. readstring ('bbb ', 'bstring', ''); {the last parameter is the default value} I: = ini. readinteger ('bbb ', 'binteger', 0); B: = ini. readbool ('bbb ', 'bboolean', false); F: = ini. readfloat ('bbb ', 'bdouble', 0); D: = ini. readdate ('bbb ', 'bdate', now); T: = ini. readtime ('bbb ', 'btime', now); DT: = ini. readdatetime ('bbb ', 'bdatetime', now); showmessage (s); {BBB-string} showmessage (inttostr (I )); {222} showmessage (booltostr (B); {-1 (true)} showmessage (floattostr (f); {3.14159265358979} showmessage (datetostr (d )); {2007-12-17} showmessage (timetostr (t); {22:06:23} showmessage (datetimetostr (DT); {2007-12-17 22:06:23} end; // read all the section names to tstrings: procedure tform1.button3click (Sender: tobject); var list: tstrings; begin list: = tstringlist. create; ini. readsections (list); showmessage (list. text); {aaa bbb ccc} List. free; end; // read all the keywords in the specified section to tstrings: Procedure tform1.button4click (Sender: tobject); var list: tstrings; begin list: = tstringlist. create; ini. readsection ('aaa', list); showmessage (list. text); {astring ainteger implements lean adate atime adatetime Adouble} List. free; end; // read all the keywords and values of the specified section to tstrings: Procedure tform1.button5click (Sender: tobject); var list: tstrings; begin list: = tstringlist. create; ini. readsectionvalues ('bbb ', list); showmessage (list. text); {bstring = BBB-string binteger = 222 bboolean = 1 bdate = 2007-12-17 btime = 22:06:23 bdatetime = 2007-12-17 22:06:23 bdouble = 3.14159265358979} List. free; end; // Delete and add procedure tform1.button6click (Sender: tobject); begin ini. deletekey ('bbb ', 'bstring'); {Delete keyword} ini. erasesection ('ccc '); {delete a section} // ini. updatefile; {save to file} {Add the section and keyword or modify the value, and write it directly} end; // other functions procedure tform1.button7click (Sender: tobject); var B: Boolean; s: string; begin B: = ini. sectionexists ('ddd '); {judge whether a section exists} B: = ini. valueexists ('aaa', 'astring'); {judge whether the value of a keyword exists} s: = ini. filename; {Get file name} end; Procedure tform1.formdestroy (Sender: tobject); begin ini. free; end.
 
  
 

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.