Using System. collections. generic; using System. diagnostics; using System. IO; using System. reflection; using System. runtime. interopServices; using System. text; namespace CSharpUtilHelpV2 {/// <summary> /// based on. NET 2.0 INI tool class // reference :/// http://www.cnblogs.com/leelike/archive/2011/01/27/1946061.html /// http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html /// </Summary> public class INIToolV2 {static string FilePath = null; /// <summary> /// default value when the read is not worthwhile /// </summary> static string ReadDefaultValue = string. empty; // <summary> // constructor // </summary> /// <param name = "filePath"> INI path eg: @ "C: \ test. ini "</param> public INIToolV2 (string filePath) {FilePath = filePath ;} /// <summary> /// declare the write operation function of the INI file /// </summary> /// <param name = "section"> section name </par Am> /// <param name = "key"> keyword </param> /// <param name = "val"> value corresponding to the keyword </param> /// <param name = "filePath"> path </param> // <returns> </returns> [DllImport ("kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath ); /// <summary> /// declare the INI file's read operation function // </summary> /// <param name = "section"> paragraph name </param>/ // <param name = "key"> keyword </param> /// <pa Ram name = "def"> default value when cannot be read </param> /// <param name = "retVal"> read value </param> /// <param name = "size"> value size> </param> /// <param name = "filePath"> path </param> /// <returns> </returns> [DllImport ("kernel32")] private static extern int GetPrivateProfileString (string section, string key, string def, StringBuilder retVal, int size, string filePath ); /// <summary> /// write INI // eg: _ iniHelper. writeValue ("test", "N Ame "," YanZhiwei "); /// </summary> /// <param name = "Section"> paragraph name </param> /// <param name = "Key"> keyword </param> /// <param name = "Value"> Value corresponding to the keyword </param> public void WriteValue (string Section, string Key, string Value) {WritePrivateProfileString (Section, Key, Value, FilePath );} /// <summary> /// read INI /// </summary> /// <param name = "Section"> Section name </param> /// <param name = "Key"> keyword </param> // <returns> Read value </returns> public string ReadValue (string Section, string Key) {StringBuilder _ valueBuilder = new StringBuilder (500); GetPrivateProfileString (Section, Key, ReadDefaultValue, _ valueBuilder, 500, filePath); return _ valueBuilder. toString ();} /// <summary> /// read INI /// </summary> /// <param name = "Section"> Section name </param> /// <param name = "Key"> keyword </param> // <param name = "defaultValue"> when the KEY cannot be read Default value </param> /// <returns> </returns> public string ReadValue (string Section, string Key, string defaultValue) {StringBuilder _ valueBuilder = new StringBuilder (500 ); getPrivateProfileString (Section, Key, defaultValue, _ valueBuilder, 500, FilePath); return _ valueBuilder. toString ();} /// <summary> /// check whether the INI file path exists /// </summary> /// <returns> </returns> public bool Exist () {if (! String. isNullOrEmpty (FilePath) {return File. exists (FilePath);} return false ;} /// <summary> /// Save the object to ini /// </summary> /// <typeparam name = "T"> generic </typeparam> // /<param name = "Section"> paragraph name </param> // <param name = "t"> type </param> public void WriteValue <T> (string Section, T t) where T: class {IDictionary <string, string> _ property = ReflectionToolV2.GetDisplayName <T> (); foreach (KeyValuePair <st Ring, string> entry in _ property) {object _ value = typeof (T ). invokeMember (entry. key, BindingFlags. getProperty, null, t, null); Trace. writeLine (_ value); if (_ value! = Null) WriteValue (Section, entry. Value, _ value. ToString ());}}}}
Test code
Code effect: