/****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:>23559055 * * Site:>http://emao.me * * ================================================================ * * 说明: * * 1.命名空间: Emao_CfgManager * 类名 : CfgManager * * 2.声明: CfgManager cm = new CfgManager(); * * 3.具体方法自己看代码吧哈。简单的很。 * * 分别是:add() readAll() writeAll() * ******************************************************************/ using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Emao_CfgManager { public class CfgManager { private Dictionary<string ,string> dict= new Dictionary<string,string>(); private string filename = Application.StartupPath + @"config.ini"; #region API [DllImport("kernel32.dll")] public extern static int GetPrivateProfileString(string segName, string keyName, string sDefault, byte[] bt, int nSize, string fileName); [DllImport("kernel32.dll")] public extern static int WritePrivateProfileString(string segName, string keyName, string sValue, string fileName); [DllImport("kernel32.dll")] public extern static int GetPrivateProfileSection(string segName, StringBuilder buffer, int nSize, string fileName); [DllImport("kernel32.dll")] public extern static int WritePrivateProfileSection(string segName, string sValue, string fileName); #endregion #region constructure public CfgManager() { ReadAll(); } public CfgManager(string inipath) { filename = inipath; ReadAll(); } #endregion #region method public int Add(object key, object value) { if (this.dict.ContainsKey(key.ToString())) this.dict[key.ToString()] = value.ToString(); else this.dict.Add(key.ToString(), value.ToString()); return this.dict.Count; } public void ReadAll() { this.dict.Clear(); //读取ini文件所有 StringBuilder sb = new StringBuilder(25500); Byte[] bt = new Byte[25000]; int len = GetPrivateProfileString("Config", null, "", bt, bt.GetUpperBound(0), filename); if (len != 0) { int start = 0; for (int i = 0; i < len; i++) { if ((bt[i] == 0) && ((i - start) > 0)) { String s = Encoding.GetEncoding(0).GetString(bt, start, i - start); Byte[] temp = new Byte[25500]; int i2 = GetPrivateProfileString("Config", s, "", temp, 25500, filename); string value = System.Text.Encoding.Default.GetString(temp).Replace('