ConfigTool.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public class ConfigTool
  15. {
  16. public static string Get(string key, string defaultValue = "")
  17. {
  18. try
  19. {
  20. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  21. return config.AppSettings.Settings[key]?.Value ?? defaultValue;
  22. }
  23. catch { return defaultValue; }
  24. }
  25. public static string GetExe(string exePath, string key, string defaultValue = "")
  26. {
  27. try
  28. {
  29. Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
  30. return config.AppSettings.Settings[key]?.Value ?? defaultValue;
  31. }
  32. catch { return defaultValue; }
  33. }
  34. public static int GetInt(string key, int defaultValue = 0)
  35. {
  36. string s = Get(key: key);
  37. if (int.TryParse(s, out int value)) return value;
  38. return defaultValue;
  39. }
  40. public static int GetExeInt(string exePath, string key, int defaultValue = 0)
  41. {
  42. string s = GetExe(exePath: exePath, key: key);
  43. if (int.TryParse(s, out int value)) return value;
  44. return defaultValue;
  45. }
  46. public static bool Set(string key, string value)
  47. {
  48. try
  49. {
  50. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  51. config.AppSettings.Settings[key].Value = value;
  52. config.Save(ConfigurationSaveMode.Modified);
  53. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  54. return true;
  55. }
  56. catch { return false; }
  57. }
  58. public static bool SetExe(string exePath, string key, string value)
  59. {
  60. try
  61. {
  62. Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
  63. config.AppSettings.Settings[key].Value = value;
  64. config.Save(ConfigurationSaveMode.Modified);
  65. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  66. return true;
  67. }
  68. catch { return false; }
  69. }
  70. //private bool CanUpdate()
  71. //{
  72. // string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
  73. // string key = "TodayUpdateTimes";
  74. // DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  75. // DateTime setday = today;
  76. // //读取配置
  77. // string temp = CanUpdateGetConfig(file, key);
  78. // if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  79. // {
  80. // if (setday.Hour < 3)
  81. // CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
  82. // else
  83. // return false;
  84. // }
  85. // else
  86. // {
  87. // //配置失效,设置为默认值
  88. // CanUpdateSetConfig(file, key, today.ToString());
  89. // }
  90. // return true;
  91. //}
  92. //private bool CanUpdateSetConfig(string file, string key, string value)
  93. //{
  94. // try
  95. // {
  96. // //文件不存在则创建
  97. // if (!File.Exists(file + ".config"))
  98. // {
  99. // XElement xe = new XElement("configuration");
  100. // xe.Save(file + ".config");
  101. // }
  102. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  103. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  104. // {
  105. // config.AppSettings.Settings[key].Value = value;
  106. // }
  107. // else
  108. // {
  109. // config.AppSettings.Settings.Add(key, value);
  110. // }
  111. // config.Save(ConfigurationSaveMode.Modified);
  112. // return true;
  113. // }
  114. // catch (Exception e)
  115. // {
  116. // return false;
  117. // }
  118. //}
  119. //private string CanUpdateGetConfig(string file, string key)
  120. //{
  121. // try
  122. // {
  123. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  124. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  125. // {
  126. // return config.AppSettings.Settings[key].Value;
  127. // }
  128. // }
  129. // catch (Exception e) { }
  130. // return null;
  131. //}
  132. }
  133. }