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