using System; using System.Runtime.InteropServices; using System.Text; namespace SKMC.Api.Common.File { /// /// Ini文件读写工具类 /// public class IniFiles { public string iniPath; [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); /// /// 构造函数 /// /// 路径 public IniFiles(string INIPath) { iniPath = INIPath; if (!System.IO.File.Exists(iniPath)) { try { System.IO.File.Create(iniPath); } catch (Exception) { } } } /// /// 向ini文件中写入值 /// /// 表头 /// 键 /// 值 public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.iniPath); } /// /// 从ini中得到int类型的值 /// /// 表头 /// 键 /// public int IniReadIntValue(string Section, string Key) { return Convert.ToInt32(IniReadStringValue(Section, Key)); } /// /// 从ini中得到double类型的值 /// /// 表头 /// 键 /// public double IniReadDoubleValue(string Section, string Key) { return Convert.ToDouble(IniReadStringValue(Section, Key)); } public bool IniReadBoolValue(string Section, string Key) { return Convert.ToBoolean(IniReadStringValue(Section, Key)); } /// /// 从ini中得到string类型的值 /// /// 表头 /// 键 /// public string IniReadStringValue(string Section, string Key) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section, Key, "", temp, 1024, this.iniPath); return temp.ToString(); } /// /// 判断文件是否存在 /// /// public bool ExistINIFile() { return System.IO.File.Exists(iniPath); } /// /// 判断键值是否相同 /// /// 表头 /// 键 /// 值 /// 设定值 public void IniWriteValue(string Section, string Key, string Value, string specBeefore) { if (Value != specBeefore) { WritePrivateProfileString(Section, Key, Value, this.iniPath); } } } }