using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;public class INIFileUtil{ ////// INI文件地址 /// private string path; ////// 初始化 /// /// 路径 private INIFileUtil(string INIPath) { this.path = INIPath; } public static INIFileUtil LoadIniFile(string iniFile) { return new INIFileUtil(iniFile); } ////// 构造函数 /// private INIFileUtil() { } [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")] private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); [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); ////// 写INI文件 /// /// 分组节点 /// 关键字 /// 值 public void IniWriteValue(string Section, string Key, string Value) { INIFileUtil.WritePrivateProfileString(Section, Key, Value, this.path); } ////// 读取INI文件 /// /// 分组节点 /// 关键字 ///public string IniReadValue(string Section, string Key) { StringBuilder stringBuilder = new StringBuilder(255); INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, 255, this.path); return stringBuilder.ToString(); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 ///public byte[] IniReadValues(string section, string key) { byte[] array = new byte[255]; INIFileUtil.GetPrivateProfileString(section, key, "", array, 255, this.path); return array; } /// /// 删除ini文件下所有段落 /// public void ClearAllSection() { this.IniWriteValue(null, null, null); } ////// 删除ini文件下指定段落下的所有键 /// /// 指定段 public void ClearSection(string Section) { this.IniWriteValue(Section, null, null); } ////// 读取一个ini里面所有的节 /// /// /// ///public int GetAllSectionNames(out List sections) { int MAX_BUFFER = 32767; IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER); int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path); if (bytesReturned == 0) { sections = new List (); return -1; } string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString(); Marshal.FreeCoTaskMem(pReturnedString); //use of Substring below removes terminating null for split sections = local.Substring(0, local.Length - 1).Split('\0').ToList (); return 0; } /// /// 得到某个节点下面所有的key和value组合 /// /// /// /// /// ///public int GetAllKeyValues(string section, out string[] keys, out string[] values) { 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); ArrayList result = new ArrayList(); foreach (string r in tmp) { if (r != string.Empty) result.Add(r); } keys = new string[result.Count]; values = new string[result.Count]; for (int i = 0; i < result.Count; i++) { string[] item = result[i].ToString().Split(new char[] { '=' }); if (item.Length == 2) { keys[i] = item[0].Trim(); values[i] = item[1].Trim(); } else if (item.Length == 1) { keys[i] = item[0].Trim(); values[i] = ""; } else if (item.Length == 0) { keys[i] = ""; values[i] = ""; } } return 0; }}