C # Read and write to INI file

Source: Internet
Author: User

C # itself does not have an operation class for the INI format file, and can customize a Inifile class to read and write INI files.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;namespacemsframework.common{/// <summary>    ///INI File class/// </summary>     Public classIniFile {Private stringM_filename;  Public stringFileName {Get{returnM_filename;} Set{M_filename =value;} } [DllImport ("Kernel32.dll")]        Private Static extern intGetprivateprofileint (stringLpappname,stringLpkeyname,intNdefault,stringlpfilename); [DllImport ("Kernel32.dll")]        Private Static extern intGetPrivateProfileString (stringLpappname,stringLpkeyname,stringLpdefault, StringBuilder lpreturnedstring,intNSize,stringlpfilename); [DllImport ("Kernel32.dll")]        Private Static extern intWritePrivateProfileString (stringLpappname,stringLpkeyname,stringlpstring,stringlpfilename); /// <summary>        ///constructor Function/// </summary>        /// <param name= "Afilename" >INI file path</param>         PublicIniFile (stringafilename) {             This. M_filename =Afilename; }        /// <summary>        ///constructor Function/// </summary>         PublicIniFile () {}/// <summary>        ///[Extended] read int value/// </summary>        /// <param name= "section" >Section</param>        /// <param name= "name" >Key</param>        /// <param name= "def" >Default Value</param>        /// <returns></returns>         Public intReadInt (stringSectionstringNameintdef) {            returnGetprivateprofileint (section, name, Def, This. M_filename); }        /// <summary>        ///[Extended] reading string strings/// </summary>        /// <param name= "section" >Section</param>        /// <param name= "name" >Key</param>        /// <param name= "def" >Default Value</param>        /// <returns></returns>         Public stringReadString (stringSectionstringNamestringdef) {StringBuilder VRETSB=NewStringBuilder (2048); GetPrivateProfileString (section, name, Def, VRETSB,2048, This. M_filename); returnvretsb.tostring (); }        /// <summary>        ///[Extended] writes an int value, and if no section-key is present, it is automatically created/// </summary>        /// <param name= "section" >Section</param>        /// <param name= "name" >Key</param>        /// <param name= "Ival" >Write Value</param>         Public voidWriteint (stringSectionstringNameintival) {writeprivateprofilestring (section, Name, Ival.tostring (), This. M_filename); }        /// <summary>        ///[Extended] writes a string string and if no section-key is present, it is automatically created/// </summary>        /// <param name= "section" >Section</param>        /// <param name= "name" >Key</param>        /// <param name= "Strval" >Write Value</param>         Public voidWriteString (stringSectionstringNamestringstrval) {writeprivateprofilestring (section, name, Strval, This. M_filename); }        /// <summary>        ///delete the specified section/// </summary>        /// <param name= "section" ></param>         Public voidDeletesection (stringSection ) {writeprivateprofilestring (section,NULL,NULL, This. M_filename); }        /// <summary>        ///Delete all sections/// </summary>         Public voiddeleteallsection () {writeprivateprofilestring (NULL,NULL,NULL, This. M_filename); }        /// <summary>        ///reads the value of the specified section-key/// </summary>        /// <param name= "section" ></param>        /// <param name= "name" ></param>        /// <returns></returns>         Public stringInireadvalue (stringSectionstringname) {StringBuilder STRSB=NewStringBuilder ( the); GetPrivateProfileString (section, name,"", STRSB, the, This. M_filename); returnstrsb.tostring (); }        /// <summary>        ///writes the specified value, and if no section-key is present, it is automatically created/// </summary>        /// <param name= "section" ></param>        /// <param name= "name" ></param>        /// <param name= "value" ></param>         Public voidIniwritevalue (stringSectionstringNamestringvalue) {writeprivateprofilestring (section, name, value, This. M_filename); }    }}

Application:

There are original INI files:

Execute code:

IniFile Myini =NewIniFile (Environment.currentdirectory +"\\Config.ini"); Myini.writeint ("tsection","Tname", the); Console.WriteLine (Myini.readstring ("System","Mainformname","")); Console.WriteLine (Myini.readstring ("DBServer","DBFile","")); Myini.writestring ("DBServer","DBFile",@"E:\Work\test2.db"); Console.WriteLine (Myini.readstring ("DBServer","DBServer","")); Console.WriteLine (Myini.readstring ("DBServer","DBFile",""));

Execution Result:

http://www.cnblogs.com/CUIT-DX037/

C # Read and write to INI file

Related Article

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.