C# ini文件操作类
/// <summary> /// INI文件读写类。 /// </summary> public class INIFile { public string path; public INIFile(string INIPath) { path = INIPath; } [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName); [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)] private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath); [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)] private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath); /// <summary> /// 写INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(1024); int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.path); return temp.ToString(); } public string IniReadValue(string Section, string Key, int size) { StringBuilder temp = new StringBuilder(size); int i = GetPrivateProfileString(Section, Key, "", temp, size, this.path); return temp.ToString(); } public int ReadInt(string Section, string Key) { int def = 0; return GetPrivateProfileInt(Section, Key, def, this.path); } public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[1024]; int i = GetPrivateProfileString(section, key, "", temp, 1024, this.path); return temp; } /// <summary> /// 删除ini文件下所有段落 /// </summary> public void ClearAllSection() { IniWriteValue(null, null, null); } /// <summary> /// 删除ini文件下personal段落下的所有键 /// </summary> /// <param name="Section"></param> public void ClearSection(string Section) { IniWriteValue(Section, null, null); } /// <summary> /// 返回指定配置文件下的节名称列表 /// </summary> /// <returns></returns> public List<string> GetAllSectionNames() { List<string> sectionList = new List<string>(); int MAX_BUFFER = 32767; IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER); int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path); if (bytesReturned != 0) { string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString(); Marshal.FreeCoTaskMem(pReturnedString); sectionList.AddRange(local.Substring(0, local.Length - 1).Split('\0')); } return sectionList; } /// <summary> /// 得到某个节点下面所有的key和value组合 /// </summary> /// <param name="section">指定的节名称</param> /// <param name="keys">Key数组</param> /// <param name="values">Value数组</param> /// <param name="path">INI文件路径</param> /// <returns></returns> public Hashtable GetAllKeyValues(string section) { Hashtable ht = new Hashtable(); byte[] b = new byte[65535];//配置节下的所有信息 GetPrivateProfileSection(section, b, b.Length, this.path); string s = System.Text.Encoding.Default.GetString(b);//配置信息 string[] tmp = s.Split((char)0);//Key\Value信息 List<string> result = new List<string>(); foreach (string r in tmp) { if (r != string.Empty) result.Add(r); } for (int i = 0; i < result.Count; i++) { string[] item = result[i].Split(new char[] { '=' });//Key=Value格式的配置信息 string key = item[0].Trim(); //Value字符串中含有=的处理, //一、Value加"",先对""处理 //二、Key后续的都为Value if (item.Length > 2) { ht.Add(key, result[i].Substring(key.Length + 1)); } if (item.Length == 2)//Key=Value { ht.Add(key, item[1].Trim()); } else if (item.Length == 1)//Key= { ht.Add(key, ""); } else if (item.Length == 0) { //ht.Add("", ""); } } return ht; } }