Encapsulate INI file read/write Functions

Source: Internet
Author: User
Delphi reads and writes the INI file instance // -- mainly implements two processes: when the form is closed, the interface information of the file is saved; when the form is created, the program reads the information stored in the file. // -- Uses inifiles (unit) // -- when the form is created, read the INI file information procedure tfrmafn04h_f9.formcreate (Sender: tobject); var vfini: Tinifile; sfilename: string; begin sfilename: = extractfiledir (application. exename); If copy (sfilename, length (sfilename)-1, 1) = '/'then sfilename: = sfilename + 'saveinformation. INI 'else sfilename: = sfilename + '/' + 'saveinformation. INI '; vfini: = Tinifile. create (sfilename); edit1. Text: = vfini. readstring ('afn04f7 ', 'edit1', '0'); edit2.text: = vfini. readstring ('afn04f7 ', 'edit2', '0'); edit3.text: = vfini. readstring ('afn04f7 ', 'edit1', '0'); edit4.text: = vfini. readstring ('afn04f7 ', 'edit4', '0'); end; // when the form is closed, the interface information of the file is saved. Procedure tfrmafn04h_f9.formclose (Sender: tobject; var action: tcloseaction); var sfilename: string; vfini: Tinifile; itmp, I: integer; Name: string; Begin // write the configuration file sfilename: = extractfiledir (application. exename); If copy (sfilename, length (sfilename)-1, 1) = '/'then sfilename: = sfilename + 'saveinformation. INI 'else sfilename: = sfilename + '/' + 'saveinformation. INI '; vfini: = Tinifile. create (sfilename); try vfini. writestring ('afn04f9', 'edit1', edit1.text); vfini. writestring ('afn04f9', 'edit2', edit2.text); vfini. writestring ('afn04f9', 'e Dit3', edit3.text); vfini. writestring ('afn04f9', 'edit4', edit4.text); finally vfini. free; end; [Delphi] view plaincopyuses inifiles; // simple data type tsimpletype = (stint, stfloat, ststring, stdatetime, stdate, sttime, stboolean ); function readinivalue (const filename, section, name: string; simpletype: tsimpletype; defaultvalue: variant): variant; // function for reading the INI file // filename: INI file name // section: node // Nam E: field name // simpletype: simple data type // defaultvalue: Default Value // returns the variant type begin with Tinifile. create (filename) Do try if simpletype = ststring then result: = readstring (section, name, defaultvalue) else if simpletype = stint then result: = readinteger (section, name, defaultvalue) else if simpletype = stfloat then result: = readfloat (section, name, defaultvalue) else if simpletype = stdatetime then result: = readda Tetime (section, name, defaultvalue) else if simpletype = stdate then result: = readdate (section, name, defaultvalue) else if simpletype = sttime then result: = readtime (section, name, defaultvalue) else if simpletype = stboolean then result: = readbool (section, name, defaultvalue); finally free; end; Procedure writeinivalue (const filename, section, name: string; value: variant; simpletype: tsimple Type); // function used to write the INI file // filename: INI file name // section: node // name: field name // value: Field Value // simpletype: simple data type: In in with Tinifile. create (filename) Do try if simpletype = ststring then writestring (section, name, vartostr (value) else if simpletype = stint then writeinteger (section, name, value) else if simpletype = stfloat then writefloat (section, name, value) else if simpletype = stdatetime then writedatetim E (section, name, vartodatetime (value) else if simpletype = stdate then writedate (section, name, vartodatetime (value) else if simpletype = sttime then writetime (section, name, vartodatetime (value) else if simpletype = stboolean then writebool (section, name, value); finally free; end; call example: writeinivalue ('C:/config. INI ', 'string', 'db', 'aaa', ststring); writeinivalue ('C:/config. INI ', 'constri Ng ', 'Port', 1, stint); readinivalue ('C:/config. INI ', 'string', 'dbname', ststring, 'misdate'); readinivalue ('C:/config. INI ', 'string', 'connect', stint,-1); writeinivalue ('C:/Chinese rotten shoes. INI ', 'cheoegisai', 'pig coach', 'Guo 12', ststring); writeinivalue ('C:/Chinese rotten shoes. INI ', 'cheoegisai', 'average penalty', 1, stint); readinivalue ('C:/Chinese rotten shoes. INI ', 'cheoegisai', 'pig coach', ststring, 'Guo 12'); readinivalue ('C:/Chinese rotten shoes. INI ', 'cheoegisai', 'average penalty', stint,-1); [Delphi] view plaincopy1.delphi To read and write an INI file, you must reference the 'inifiles 'Unit 2. variable: var filename: string; myinifile: Tinifile; 3. variable value filename: = extractfilepath (paramstr (0) + 'program. INI '; // specify the path. if no path is specified. the file will be created in the Windows directory myinifile: = Tinifile. create (filename); // create ('program. INI '); 4. write the file myinifile. writestring (segment name, keyword, variable or string value); myinifile. writeinteger (segment name, keyword, variable or integer value); myinifile. writenbool (section name, keyword, variable or true/false); If the INI file does not exist. the INI file is automatically created. delete the keyword myin Ifile. deletekey (segment name, keyword); 6. delete section myinifile. erasesection (section name); 7. release the variable myinifile. distory; 8. other Tinifile classes also provide three object methods to operate INI files ① myinifile. readsection (section 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 (the section name, tstrings variable); all rows (including keywords, =, and values) of the specified section in the INI file can be read to a string list variable. 9. delphi reads and writes the INI file instance // -- mainly implements two processes: when the form is closed, the interface information of the file is saved; when the form is created, The program reads the information stored in the file. // -- Uses inifiles (unit) // -- when the form is created, read the INI file information procedure tfrmafn04h_f9.formcreate (Sender: tobject); var vfini: Tinifile; sfilename: string; begin sfilename: = extractfiledir (application. exename); If copy (sfilename, length (sfilename)-1, 1) = '/'then sfilename: = sfilename + 'saveinformation. INI 'else sfilename: = sfilename + '/' + 'saveinformation. INI '; vfini: = Tinifile. create (sfilename); edit1.text: = vfini. readstring ('afn04f7 ', 'edit1', '0'); edit2.text: = vfini. readstring ('afn04f7 ', 'edit2', '0'); edit3.text: = vfini. readstring ('afn04f7 ', 'edit1', '0'); edit4.text: = vfini. readstring ('afn04f7 ', 'edit4', '0'); end; // when the form is closed, the interface information of the file is saved. Procedure tfrmafn04h_f9.formclose (Sender: tobject; var action: tcloseaction); var sfilename: string; vfini: Tinifile; itmp, I: integer; Name: string; begin // write the configuration file sfilename: = extractfiledir (application. exename); If copy (sfilename, length (sfilename)-1, 1) = '/'then sfilename: = sfilename + 'saveinformation. INI 'else sfilename: = sfilename + '/' + 'saveinformation. INI '; vfini: = Tinifile. create (sfilename); try vfini. writestring ('afn04f9', 'edit1', edit1.text); vfini. writestring ('afn04f9', 'edit2', edit2.text); vfini. writestring ('afn04f9', 'edit3', edit3.text); vfini. writestring ('afn04f9', 'edit4', edit4.text); finally vfini. free; end;

  

Encapsulate INI file read/write Functions

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.