INI file read and write operations

Source: Internet
Author: User
Tags read ini file

To read and write INI files in C #, a using System.Runtime.InteropServices is introduced here; namespaces, in the following ways:

  #region variable private static readonly string strFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString () + "App . ini ";//ini file path #endregion #region Private method///<summary>//write INI file//</summ ary>//<param name= "section" > node name [e.g. [typename]]</param>//<param name= "key" > Key </pa        ram>//<param name= "val" > Value </param>//<param name= "filepath" > File path </param> <returns></returns> [DllImport ("kernel32")] private static extern long Writeprivateprofil        Estring (string section, string key, String val, string filepath); <summary>///Read INI file///</summary>//<param name= "section" > Node name &LT;/PARAM&G        T <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 int getprivateprofilestring (string section, Strin        G Key, String Def, StringBuilder retval, int size, string filePath);             #endregion #region Public method publicly static string GetValue (string section, string key) {try                {int size = 2048;                StringBuilder temp = new StringBuilder (size);                GetPrivateProfileString (section, Key, "", temp, size, strFilePath); Return temp.            ToString ();            } catch (Exception e) {throw new Exception (e.message);            }} public static bool WriteValue (string section, string key, String value) {Try {Long length= writeprivateprofilestring (section, key, value, strFilePath);            Return length>0;            } catch (Exception e) {throw new Exception (e.message); }} #endregion

  

INI file read and write operations

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.