using System; using System.Runtime.InteropServices; using System.Text; namespace Masuit.Tools.Files { /// /// INI文件操作辅助类,仅支持Windows系统 /// public class INIFile { /// /// 文件路径 /// public readonly string path; /// /// 传入INI文件路径构造对象 /// /// INI文件路径 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); /// /// 写INI文件 /// /// 分组节点 /// 关键字 /// 值 public void IniWriteValue(string section, string key, string value) { WritePrivateProfileString(section, key, value, path); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 /// public string IniReadValue(string section, string key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(section, key, "", temp, 255, path); return temp.ToString(); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 /// 值的字节表现形式 public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[255]; int i = GetPrivateProfileString(section, key, "", temp, 255, path); return temp; } /// /// 删除ini文件下所有段落 /// public void ClearAllSection() { IniWriteValue(null, null, null); } /// /// 删除ini文件下指定段落下的所有键 /// /// 分组节点 public void ClearSection(string section) { IniWriteValue(section, null, null); } } }