[DllImport ("Kernel32.dll")] private static extern bool WritePrivateProfileString (string section, string key, String val, string filePath); [DllImport ("Kernel32.dll")] private static extern int getprivateprofilestring (string section, string key, String de F, StringBuilder retVal, int size, string filePath); private string spath_ = ""; <summary>//absolute path///</summary> public string Spath {get {retur n Spath_; } set {spath_ = value;} }///<summary>///write to INI file///</summary>//<param name= "section" > File Domain & lt;/param>//<param name= "key" >key value </param>///<param Name= "value" >value value </param& Gt <returns></returns> public bool Writetoini (string section, string key, String value) { BOOL result = FALSE; if (System.IO.File.Exists (spath){result = WritePrivateProfileString (section, key, value, spath); } return result; }///<summary>///read from INI file///</summary>//<param name= "section" > File Domain & lt;/param>//<param name= "key" >key value </param>///<param Name= "defaultvalue" > Default </pa ram>//<returns></returns> public string Readfromini (string section, String key, String def Aultvalue = "") {string result = DefaultValue; if (System.IO.File.Exists (spath)) {StringBuilder stemp = new StringBuilder (1024); int i = getprivateprofilestring (section, key, DefaultValue, Stemp, 1024x768, spath); result = Stemp.tostring (); } return result; }
Strongly NOTE:
INI file must be in ANSI, Unicode format, or an exception will occur, for reasons that are not found yet. Please help the hero to explain.
Read in C #, write INI file