一个IniHelper

读写Ini文件,可以使用最直接的方法——文件IO,可是分析ini文件里的各个节、参数又要自己分析,使用流时又要注意资源的释放,故懒人就用系统的API了。这个Helper类一点也不复杂。

 1         [DllImport("kernel32")]
 2         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 3 
 4         [DllImport("kernel32")]
 5         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 6 
 7         public static string IniReadValue(string filePath, string section, string key)
 8         {
 9             StringBuilder sb = new StringBuilder(255);
10             GetPrivateProfileString(section, key, "", sb, 255, filePath);
11             return sb.ToString();
12         }
13 
14         public static void IniWriteValue(string filePath,string section, string key, string value)
15         {
16             WritePrivateProfileString(section, key, value, filePath);
17         }
原文地址:https://www.cnblogs.com/HopeGi/p/2982469.html