ConfigTool.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Linq;
  11. using System.Text;
  12. namespace Azylee.Core.IOUtils.TxtUtils
  13. {
  14. /// <summary>
  15. /// .NET 读取配置工具
  16. /// </summary>
  17. public class ConfigTool
  18. {
  19. /// <summary>
  20. /// 获取配置值
  21. /// </summary>
  22. /// <param name="key"></param>
  23. /// <param name="defaultValue"></param>
  24. /// <returns></returns>
  25. public static string Get(string key, string defaultValue = "")
  26. {
  27. try
  28. {
  29. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  30. return config.AppSettings.Settings[key]?.Value ?? defaultValue;
  31. }
  32. catch { return defaultValue; }
  33. }
  34. /// <summary>
  35. /// 获取配置值(bool)
  36. /// </summary>
  37. /// <param name="key"></param>
  38. /// <param name="defaultValue"></param>
  39. /// <returns></returns>
  40. public static bool GetBool(string key, bool defaultValue = false)
  41. {
  42. string value = Get(key, "");
  43. if (Str.Ok(value))
  44. {
  45. value = value.ToLower();
  46. switch (value)
  47. {
  48. case "1":
  49. case "ok":
  50. case "yes":
  51. case "true":
  52. case "enable":
  53. case "access": return true;
  54. default: return false;
  55. }
  56. }
  57. return defaultValue;
  58. }
  59. public static string GetExe(string exePath, string key, string defaultValue = "")
  60. {
  61. try
  62. {
  63. Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
  64. return config.AppSettings.Settings[key]?.Value ?? defaultValue;
  65. }
  66. catch { return defaultValue; }
  67. }
  68. public static int GetInt(string key, int defaultValue = 0)
  69. {
  70. string s = Get(key: key);
  71. if (int.TryParse(s, out int value)) return value;
  72. return defaultValue;
  73. }
  74. public static int GetExeInt(string exePath, string key, int defaultValue = 0)
  75. {
  76. string s = GetExe(exePath: exePath, key: key);
  77. if (int.TryParse(s, out int value)) return value;
  78. return defaultValue;
  79. }
  80. public static bool Set(string key, string value)
  81. {
  82. try
  83. {
  84. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  85. config.AppSettings.Settings[key].Value = value;
  86. config.Save(ConfigurationSaveMode.Modified);
  87. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  88. return true;
  89. }
  90. catch { return false; }
  91. }
  92. public static bool SetExe(string exePath, string key, string value)
  93. {
  94. try
  95. {
  96. Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
  97. config.AppSettings.Settings[key].Value = value;
  98. config.Save(ConfigurationSaveMode.Modified);
  99. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  100. return true;
  101. }
  102. catch { return false; }
  103. }
  104. //private bool CanUpdate()
  105. //{
  106. // string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
  107. // string key = "TodayUpdateTimes";
  108. // DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  109. // DateTime setday = today;
  110. // //读取配置
  111. // string temp = CanUpdateGetConfig(file, key);
  112. // if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  113. // {
  114. // if (setday.Hour < 3)
  115. // CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
  116. // else
  117. // return false;
  118. // }
  119. // else
  120. // {
  121. // //配置失效,设置为默认值
  122. // CanUpdateSetConfig(file, key, today.ToString());
  123. // }
  124. // return true;
  125. //}
  126. //private bool CanUpdateSetConfig(string file, string key, string value)
  127. //{
  128. // try
  129. // {
  130. // //文件不存在则创建
  131. // if (!File.Exists(file + ".config"))
  132. // {
  133. // XElement xe = new XElement("configuration");
  134. // xe.Save(file + ".config");
  135. // }
  136. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  137. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  138. // {
  139. // config.AppSettings.Settings[key].Value = value;
  140. // }
  141. // else
  142. // {
  143. // config.AppSettings.Settings.Add(key, value);
  144. // }
  145. // config.Save(ConfigurationSaveMode.Modified);
  146. // return true;
  147. // }
  148. // catch (Exception e)
  149. // {
  150. // return false;
  151. // }
  152. //}
  153. //private string CanUpdateGetConfig(string file, string key)
  154. //{
  155. // try
  156. // {
  157. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  158. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  159. // {
  160. // return config.AppSettings.Settings[key].Value;
  161. // }
  162. // }
  163. // catch (Exception e) { }
  164. // return null;
  165. //}
  166. }
  167. }