using System; using System.Runtime.InteropServices; using System.Text; namespace Masuit.Tools.Files { /// /// INI文件操作辅助类 /// 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, this.path); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 /// public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString(); } /// /// 读取INI文件 /// /// 分组节点 /// 关键字 /// 值的字节表现形式 public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[255]; int i = GetPrivateProfileString(section, key, "", temp, 255, this.path); return temp; } /// /// 删除ini文件下所有段落 /// public void ClearAllSection() { IniWriteValue(null, null, null); } /// /// 删除ini文件下指定段落下的所有键 /// /// 分组节点 public void ClearSection(string Section) { IniWriteValue(Section, null, null); } } }