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 </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