using System.Runtime.InteropServices; using System.Text; namespace SiteCore { public static class IniHelper { [DllImport("kernel32")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32")] private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); public static string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + "Config.ini"; /// /// 读取INI文件值 /// /// 节点名 /// 键 /// 未取到值时返回的默认值 /// INI文件完整路径 /// 读取的值 public static string Read(string section, string key, string def) { StringBuilder sb = new StringBuilder(1024); GetPrivateProfileString(section, key, def, sb, 1024, FilePath); return sb.ToString(); } /// /// 写INI文件值 /// /// 欲在其中写入的节点名称 /// 欲设置的项名 /// 要写入的新字符串 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int Write(string section, string key, string value) { return WritePrivateProfileString(section, key, value, FilePath); } /// /// 删除节 /// /// 节点名 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int DeleteSection(string section) { return Write(section, null, null); } /// /// 删除键的值 /// /// 节点名 /// 键名 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int DeleteKey(string section, string key) { return Write(section, key, null); } } }