C # operation INI file

Source: Internet
Author: User
Tags read ini file

C # Create INI file, read values, modify values

Call Method:

int ibaudrate = inihelper.readconfig<int> ("com_setup"" baud rate ")
Inihelper class:
1     Public classInihelper2     {3         Private Static stringFileName = Application.startuppath +"\\AppConfig.ini";4         /// <summary>5         ///reading configuration Files6         /// </summary>7         /// <typeparam name= "T" ></typeparam>8         /// <param name= "section" ></param>9         /// <param name= "key" ></param>Ten         /// <returns></returns> One          Public StaticT readconfig<t> (stringSectionstringkey) A         { -             if(File.exists (FileName)) -             { theIniFile f =NewIniFile (FileName); -                 stringValue =f.readcontentvalue (section, key); -  -                 if(String.isnullorwhitespace (value)) +                     return default(T); -  +                 if(typeof(T). Isenum) A                     returnT Enum.parse (typeof(T), value,true); at  -                 returnT Convert.changetype (Value,typeof(T)); -             } -             Else -             { -                 return default(T); in             } -         } to  +         /// <summary> -         ///Write config file the         /// </summary> *         /// <param name= "section" ></param> $         /// <param name= "key" ></param>Panax Notoginseng         /// <param name= "value" ></param> -          Public Static voidWriteconfig (stringSectionstringKeystringvalue) the         { +             //If the file does not exist, create the A             if(!file.exists (FileName)) the             { +                 using(FileStream MYFS =NewFileStream (FileName, FileMode.Create)) { } -             } $  $IniFile f =NewIniFile (FileName); - f.writecontentvalue (section, key, value); -         } the}

Core code:

  Public classIniFile { Public stringPath;  PublicIniFile (stringpath) {             This. Path =path; }        /// <summary>        ///Write INI file/// </summary>        /// <param name= "section" >node name [e.g. [TypeName]]</param>        /// <param name= "key" >Key</param>        /// <param name= "val" >value</param>        /// <param name= "filepath" >file path</param>        /// <returns></returns>[DllImport ("kernel32")]        Private Static extern LongWritePrivateProfileString (stringSectionstringKeystringValstringfilepath); /// <summary>        ///Read INI file/// </summary>        /// <param name= "section" >Node name</param>        /// <param name= "key" >Key</param>        /// <param name= "def" >value</param>        /// <param name= "RetVal" >Stringbulider Object</param>        /// <param name= "Size" >byte size</param>        /// <param name= "FilePath" >file path</param>        /// <returns></returns>[DllImport ("kernel32")]        Private Static extern intGetPrivateProfileString (stringSectionstringKeystringDef, StringBuilder retval,intSizestringFilePath); /// <summary>        ///Write/// </summary>        /// <param name= "section" ></param>        /// <param name= "key" ></param>        /// <param name= "Ivalue" ></param>         Public voidWritecontentvalue (stringSectionstringKeystringivalue) {writeprivateprofilestring (section, Key, Ivalue, This.        Path); }        /// <summary>        ///read the content method in the INI file/// </summary>        /// <param name= "section" >Key</param>        /// <param name= "key" >value</param>        /// <returns></returns>         Public stringReadcontentvalue (stringSection,stringkey) {StringBuilder temp=NewStringBuilder (1024x768); GetPrivateProfileString (section, key,"", temp,1024x768, This.            Path); returntemp.        ToString (); }    }

C # operation 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.