C # Read the INI configuration file class

Source: Internet
Author: User

Files with configuration files of filename.ini

The first line must be empty, otherwise the value cannot be read
[Section1]key=Valuekey2=Value ... [Section2]key=Valuekey2=value ...

The code is as follows:

usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Text;namespacetest{/// <summary>    ///INI file Operation class/// </summary>     Public classIniFile { Public stringPath;  PublicIniFile (stringpath) {             This. Path =path; }                #regionAPI functions declaring read-write INI files[DllImport ("kernel32")]         Private Static extern LongWritePrivateProfileString (stringSectionstringKeystringValstringFilePath); [DllImport ("kernel32")]        Private Static extern intGetPrivateProfileString (stringSectionstringKeystringDefval, StringBuilder RetVal,intSizestringFilePath); [DllImport ("kernel32")]        Private Static extern intGetPrivateProfileString (stringSectionstringKeystringDefval, byte[] RetVal,intSizestringFilePath); #endregion        /// <summary>        ///Write INI file/// </summary>        /// <param name= "section" >paragraph</param>        /// <param name= "key" >Key</param>        /// <param name= "Ivalue" >value</param>         Public voidIniwritevalue (stringSectionstringKeystringivalue) {writeprivateprofilestring (section, Key, Ivalue, This.        Path); }        /// <summary>        ///Read INI file/// </summary>        /// <param name= "section" >paragraph</param>        /// <param name= "key" >Key</param>        /// <returns>the returned key value</returns>         Public stringInireadvalue (stringSectionstringkey) {StringBuilder temp=NewStringBuilder (255); inti = getprivateprofilestring (section, Key,"", temp,255, This.             Path); returntemp.        ToString (); }        /// <summary>        ///Read INI file/// </summary>        /// <param name= "section" >segment, format []</param>        /// <param name= "Key" >Key</param>        /// <returns>returns a section group or a key value group of type byte</returns>         Public byte[] Inireadvalues (stringSectionstringkey) {            byte[] temp =New byte[255]; inti = getprivateprofilestring (section, Key,"", temp,255, This.            Path); returntemp; }    }}

C # Read the INI configuration file class

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.