using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace Rs.Framework { public class IniHelper { /// /// Ini文件完整路径 /// public string IniPath { get; set; } /// /// 构造方法 /// public IniHelper() { } /// /// 构造方法 /// /// Ini文件的完整路径,包括文件名 public IniHelper(string path) { IniPath = path; } #region win32函数 /// /// 写操作 /// /// Ini文件中的段落 /// Ini文件中的关键字 /// 关键字的值 /// Ini文件的完整路径 /// [DllImport("kernel32")] private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath); /// /// 读操作 /// /// Ini文件中的段落 /// Ini文件中的关键字 /// 无法读取时的缺省值 /// 读取的值 /// 值的大小 /// Ini文件的完整路径 /// [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); #endregion /// /// Ini文件写入 /// /// Ini文件段落 /// /// 值 public void WriteValue(string section, string key, string value) { bool b = WritePrivateProfileString(section, key, value, IniPath); if (!b) throw new Exception("Ini文件写入错误"); } /// /// 读取指定的Section和Ident的值 /// /// Ini文件段落 /// 关键字 /// public string ReadValue(string section, string key) { return ReadValue(section, key, ""); } /// /// 读取指定的Section和Ident的值 /// /// Ini文件段落 /// 关键字 /// 读取失败时的默认值 /// public string ReadValue(string section, string key, string defaultValue) { byte[] buffer = new byte[65535]; int bufLen = GetPrivateProfileString(section, key, defaultValue, buffer, buffer.GetUpperBound(0), IniPath); string s = Encoding.GetEncoding(0).GetString(buffer); s = s.Substring(0, bufLen); return s.Trim(); } /// /// 获取指定Section下的所有Ident /// /// /// public void ReadSection(string section, StringCollection keys) { byte[] buffer = new byte[16384]; int bufLen = GetPrivateProfileString(section, null, null, buffer, buffer.GetUpperBound(0), IniPath); GetStringsFromBuffer(buffer, bufLen, keys); } /// /// 解析Section /// /// /// /// private static void GetStringsFromBuffer(byte[] buffer, int bufLen, StringCollection keys) { keys.Clear(); if (bufLen != 0) { int start = 0; for (int i = 0; i < bufLen; i++) { if ((buffer[i] == 0) && ((i - start) > 0)) { string s = Encoding.GetEncoding(0).GetString(buffer, start, i - start); keys.Add(s); start = i + 1; } } } } /// /// 获取Sections /// /// public void ReadSections(StringCollection sections) { byte[] buffer = new byte[65535]; int bufLen = GetPrivateProfileString(null, null, null, buffer, buffer.GetUpperBound(0), IniPath); GetStringsFromBuffer(buffer, bufLen, sections); } /// /// 获取指定Section下的所有Value /// /// /// public void ReadSectionValues(string section, NameValueCollection values) { StringCollection idents = new StringCollection(); ReadSection(section, idents); values.Clear(); foreach (string ident in idents) { values.Add(ident, ReadValue(section, ident, "")); } } /// /// 清除指定的Section /// /// public void EraseSection(string section) { bool b = WritePrivateProfileString(section, null, null, IniPath); if (!b) throw new Exception("无法清除Ini文件中的Section"); } /// /// 删除指定Section下的指定键 /// /// /// public void DeleteIdent(string section, string key) { bool b = WritePrivateProfileString(section, key, null, IniPath); if (!b) throw new Exception("无法删除指定的Ident"); } /// /// 检查指定的键是否有值 /// /// /// /// public bool ValueExists(string section, string key) { StringCollection keys = new StringCollection(); ReadSection(section, keys); return keys.IndexOf(key) > -1; } /// /// 执行完对Ini文件的操作后, /// 调用本方法更新缓冲区, /// 对于Win9X,执行此方法, /// 对于Win NT及更高的系统上,则不需执行此方法 /// public void UpdateFile() { WritePrivateProfileString(null, null, null, IniPath); } /// /// 验证文件是否存在 /// /// public bool ExistIniFile { get { if (string.IsNullOrEmpty(IniPath)) throw new ArgumentException("IniPath"); return File.Exists(IniPath); } } ~IniHelper() { UpdateFile(); } } }