using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; public class INIFileUtil { /// <summary> /// INI文件地址 /// </summary> private string path; /// <summary> /// 初始化 /// </summary> /// <param name="INIPath">路径</param> private INIFileUtil(string INIPath) { this.path = INIPath; } public static INIFileUtil LoadIniFile(string iniFile) { return new INIFileUtil(iniFile); } /// <summary> /// 构造函数 /// </summary> 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); /// <summary> /// 写INI文件 /// </summary> /// <param name="Section">分组节点</param> /// <param name="Key">关键字</param> /// <param name="Value">值</param> public void IniWriteValue(string Section, string Key, string Value) { INIFileUtil.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 stringBuilder = new StringBuilder(255); INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, 255, this.path); return stringBuilder.ToString(); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="section">分组节点</param> /// <param name="key">关键字</param> /// <returns></returns> public byte[] IniReadValues(string section, string key) { byte[] array = new byte[255]; INIFileUtil.GetPrivateProfileString(section, key, "", array, 255, this.path); return array; } /// <summary> /// 删除ini文件下所有段落 /// </summary> public void ClearAllSection() { this.IniWriteValue(null, null, null); } /// <summary> /// 删除ini文件下指定段落下的所有键 /// </summary> /// <param name="Section">指定段</param> public void ClearSection(string Section) { this.IniWriteValue(Section, null, null); } /// <summary> /// 读取一个ini里面所有的节 /// </summary> /// <param name="sections"></param> /// <param name="path"></param> /// <returns></returns> public int GetAllSectionNames(out List<string> 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<string>(); 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('