| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace SKMC.Api.Common.File
- {
- /// <summary>
- /// Ini文件读写工具类
- /// </summary>
- 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);
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="INIPath">路径</param>
- public IniFiles(string INIPath)
- {
- iniPath = INIPath;
- if (!System.IO.File.Exists(iniPath))
- {
- try
- {
- System.IO.File.Create(iniPath);
- }
- catch (Exception)
- { }
- }
- }
- /// <summary>
- /// 向ini文件中写入值
- /// </summary>
- /// <param name="Section">表头</param>
- /// <param name="Key">键</param>
- /// <param name="Value">值</param>
- public void IniWriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.iniPath);
- }
- /// <summary>
- /// 从ini中得到int类型的值
- /// </summary>
- /// <param name="Section">表头</param>
- /// <param name="Key">键</param>
- /// <returns></returns>
- public int IniReadIntValue(string Section, string Key)
- {
- return Convert.ToInt32(IniReadStringValue(Section, Key));
- }
- /// <summary>
- /// 从ini中得到double类型的值
- /// </summary>
- /// <param name="Section">表头</param>
- /// <param name="Key">键</param>
- /// <returns></returns>
- 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));
- }
- /// <summary>
- /// 从ini中得到string类型的值
- /// </summary>
- /// <param name="Section">表头</param>
- /// <param name="Key">键</param>
- /// <returns></returns>
- public string IniReadStringValue(string Section, string Key)
- {
- StringBuilder temp = new StringBuilder(1024);
- GetPrivateProfileString(Section, Key, "", temp, 1024, this.iniPath);
- return temp.ToString();
- }
- /// <summary>
- /// 判断文件是否存在
- /// </summary>
- /// <returns></returns>
- public bool ExistINIFile()
- {
- return System.IO.File.Exists(iniPath);
- }
- /// <summary>
- /// 判断键值是否相同
- /// </summary>
- /// <param name="Section">表头</param>
- /// <param name="Key">键</param>
- /// <param name="Value">值</param>
- /// <param name="specBeefore">设定值</param>
- public void IniWriteValue(string Section, string Key, string Value, string specBeefore)
- {
- if (Value != specBeefore)
- {
- WritePrivateProfileString(Section, Key, Value, this.iniPath);
- }
- }
- }
- }
|