ConfigTool.cs 6.4 KB

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