C#读写ini文件
方式一,用Windows API
using System.Runtime.InteropServices; using System.Text; namespace Configure { class IniConfigHelper { private const int VALUE_MAX_LEN = 1024; //值最大长度 /// <summary> /// 写配置到ini文件 /// </summary> /// <param name="appName">节点名</param> /// <param name="keyName">要写入的key</param> /// <param name="value">要写入的值</param> /// <param name="fileName">要写入ini配置文件路径</param> /// <returns>成功返回非0,失败返回0</returns> [DllImport("kernel32")] private static extern long WritePrivateProfileString(string appName, string keyName, string value, string fileName); /// <summary> /// 从ini配置文件读配置 /// </summary> /// <param name="appName">节点名</param> /// <param name="keyName">目标key</param> /// <param name="defaultValue">如果找不到目标key就返回这个默认值</param> /// <param name="returnedString">返回目标key对应的值</param> /// <param name="size">returnedString的大小</param> /// <param name="fileName">要读取的ini配置文件路径</param> /// <returns>返回返回字符串的长度</returns> [DllImport("kernel32")] private static extern long GetPrivateProfileString(string appName, string keyName, string defaultValue, StringBuilder returnedString, int size, string fileName); /// <summary> /// 外部接口,写配置 /// </summary> /// <param name="iniFilePath">ini配置文件路径</param> /// <param name="appName">节点名</param> /// <param name="keyName">key</param> /// <param name="value">值</param> /// <returns>成功返回true,失败返回false</returns> public static bool WriteSetting(string iniFilePath, string appName, string keyName, string value) { return (0 == WritePrivateProfileString(appName, keyName, value, iniFilePath) ? false : true); } /// <summary> /// 读配置 /// </summary> /// <param name="iniFilePath">ini配置文件路径</param> /// <param name="appName">节点名</param> /// <param name="keyName">目标key</param> /// <param name="defaultValue">默认值</param> /// <returns>返回key对应的value</returns> public static string ReadSetting(string iniFilePath, string appName, string keyName, string defaultValue) { StringBuilder result = new StringBuilder(VALUE_MAX_LEN + 1); GetPrivateProfileString(appName, keyName, defaultValue, result, VALUE_MAX_LEN, iniFilePath); return result.ToString(); } } }
方式二,自己写
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestReadWriteIniFile { public class IniConfigHelper2 { // 获取key对应的值 public static bool GetIniKeyString(string filename, string section, string key, ref string val, ref string sErr) { // 用来标记是否找到section int flag = 0; string sSection = "[" + section + "]"; try { // 创建一个 StreamReader 的实例来读取文件 // using 语句也能关闭 StreamReader //using (StreamReader sr = new StreamReader("test.ini", Encoding.UTF8)) using (StreamReader sr = new StreamReader(filename)) { string line; // 从文件读取并显示行,直到文件的末尾 while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); int i = line.IndexOf('='); if ((-1 != i) && (1 == flag)) { string k = line.Substring(0, i); if (k.Trim() == key) { val = line.Substring(i + 1).Trim(); sErr = ""; return true; } } else { if (line.Trim() == sSection) { flag = 1; } } } } } catch (Exception e) { sErr = e.Message; } return false; } // 修改key对应的值 public static bool PutIniKeyString(string filename, string section, string key, string val, ref string sErr) { // 用来标记是否找到section int flag = 0; string sSection = "[" + section + "]"; try { StreamReader sr = new StreamReader(filename); // 临时文件名 string tmpFileName = filename + ".tmp"; StreamWriter sw = new StreamWriter(tmpFileName); // 从文件读取并显示行,直到文件的末尾 string line; while ((line = sr.ReadLine()) != null) { if (2 != flag) { int i = line.IndexOf('='); if ((-1 != i) && (1 == flag)) { if (key == line.Substring(0, i).Trim()) { // 找到对应的key flag = 2; line = key + " = " + val; } } else { if (line.Trim() == sSection) { // 找到section的位置 flag = 1; } } } // 写入临时文件 sw.WriteLine(line); } if (1 == flag) { line = key + " = " + val; sw.WriteLine(line); } if (0 == flag) { sw.WriteLine(sSection); line = key + " = " + val; sw.WriteLine(line); } sr.Close(); sw.Close(); // 删除源文件 File.Delete(filename); // 临时文件 重命名为 源文件 File.Move(tmpFileName, filename); return true; } catch (Exception e) { sErr = e.Message; } return false; } } } // // 测试代码 string val=null, sErr=null; bool b = IniConfigHelper2.GetIniKeyString(filepath, "DB", "username", ref val, ref sErr) val += "123"; bool b = IniConfigHelper2.PutIniKeyString(filepath, "book2", "name2", "爱我中华", ref sErr);
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。