IniFiles.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace SKMC.Api.Common.File
  5. {
  6. /// <summary>
  7. /// Ini文件读写工具类
  8. /// </summary>
  9. public class IniFiles
  10. {
  11. public string iniPath;
  12. [DllImport("kernel32")]
  13. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  14. [DllImport("kernel32")]
  15. public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  16. /// <summary>
  17. /// 构造函数
  18. /// </summary>
  19. /// <param name="INIPath">路径</param>
  20. public IniFiles(string INIPath)
  21. {
  22. iniPath = INIPath;
  23. if (!System.IO.File.Exists(iniPath))
  24. {
  25. try
  26. {
  27. System.IO.File.Create(iniPath);
  28. }
  29. catch (Exception)
  30. { }
  31. }
  32. }
  33. /// <summary>
  34. /// 向ini文件中写入值
  35. /// </summary>
  36. /// <param name="Section">表头</param>
  37. /// <param name="Key">键</param>
  38. /// <param name="Value">值</param>
  39. public void IniWriteValue(string Section, string Key, string Value)
  40. {
  41. WritePrivateProfileString(Section, Key, Value, this.iniPath);
  42. }
  43. /// <summary>
  44. /// 从ini中得到int类型的值
  45. /// </summary>
  46. /// <param name="Section">表头</param>
  47. /// <param name="Key">键</param>
  48. /// <returns></returns>
  49. public int IniReadIntValue(string Section, string Key)
  50. {
  51. return Convert.ToInt32(IniReadStringValue(Section, Key));
  52. }
  53. /// <summary>
  54. /// 从ini中得到double类型的值
  55. /// </summary>
  56. /// <param name="Section">表头</param>
  57. /// <param name="Key">键</param>
  58. /// <returns></returns>
  59. public double IniReadDoubleValue(string Section, string Key)
  60. {
  61. return Convert.ToDouble(IniReadStringValue(Section, Key));
  62. }
  63. public bool IniReadBoolValue(string Section, string Key)
  64. {
  65. return Convert.ToBoolean(IniReadStringValue(Section, Key));
  66. }
  67. /// <summary>
  68. /// 从ini中得到string类型的值
  69. /// </summary>
  70. /// <param name="Section">表头</param>
  71. /// <param name="Key">键</param>
  72. /// <returns></returns>
  73. public string IniReadStringValue(string Section, string Key)
  74. {
  75. StringBuilder temp = new StringBuilder(1024);
  76. GetPrivateProfileString(Section, Key, "", temp, 1024, this.iniPath);
  77. return temp.ToString();
  78. }
  79. /// <summary>
  80. /// 判断文件是否存在
  81. /// </summary>
  82. /// <returns></returns>
  83. public bool ExistINIFile()
  84. {
  85. return System.IO.File.Exists(iniPath);
  86. }
  87. /// <summary>
  88. /// 判断键值是否相同
  89. /// </summary>
  90. /// <param name="Section">表头</param>
  91. /// <param name="Key">键</param>
  92. /// <param name="Value">值</param>
  93. /// <param name="specBeefore">设定值</param>
  94. public void IniWriteValue(string Section, string Key, string Value, string specBeefore)
  95. {
  96. if (Value != specBeefore)
  97. {
  98. WritePrivateProfileString(Section, Key, Value, this.iniPath);
  99. }
  100. }
  101. }
  102. }