ToolConfig.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.ComponentModel;
  8. using Ips.Library.Basic;
  9. namespace Ips.Library.DxpLib
  10. {
  11. public class ToolConfig
  12. {
  13. public static string GetAppSetting(string key, string defaultVal = "")
  14. {
  15. string val = ConfigurationManager.AppSettings[key];
  16. return val.IfNullOrWhitespace(defaultVal);
  17. }
  18. /// <summary>
  19. /// 获取App.config配置文件中appSettings节点中指定key的value
  20. /// <para>如果泛型为bool,则{"1","true","True","TRUE"}都会被转换成true,否则转换为false</para>
  21. /// <para>该方法支持int?、double?等可空类型的转换</para>
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="key"></param>
  25. /// <param name="defaultVal"></param>
  26. /// <returns></returns>
  27. public static T GetAppSetting<T>(string key, T defaultVal = default)
  28. {
  29. var str = System.Configuration.ConfigurationManager.AppSettings[key];
  30. if (string.IsNullOrWhiteSpace(str))
  31. {
  32. return defaultVal;
  33. }
  34. str = str.Trim();
  35. bool isNullable = typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>);
  36. if (isNullable)
  37. {
  38. NullableConverter nullableConverter = new NullableConverter(typeof(T));
  39. return (T)nullableConverter.ConvertFromString(str);
  40. }
  41. else if (typeof(T) == typeof(bool))
  42. {
  43. if (str.ToLower().Trim() == "true" || str.ToLower().Trim() == "1")
  44. {
  45. T boolVal = (T)Convert.ChangeType(true, typeof(T));
  46. return boolVal;
  47. }
  48. else
  49. {
  50. T boolVal = (T)Convert.ChangeType(false, typeof(T));
  51. return boolVal;
  52. }
  53. }
  54. else
  55. {
  56. T ret = (T)Convert.ChangeType(str, typeof(T));
  57. return ret;
  58. }
  59. }
  60. public static void SetAppSetting(string key, string value)
  61. {
  62. Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  63. if (ConfigurationManager.AppSettings[key] == null)
  64. {
  65. configuration.AppSettings.Settings.Add(key, value);
  66. }
  67. else
  68. {
  69. configuration.AppSettings.Settings[key].Value = value;
  70. }
  71. RefreshAndSaveSection(configuration);
  72. }
  73. public static T GetAppSetting<T>(string key) where T : struct
  74. {
  75. var @string = ConfigurationManager.AppSettings[key];
  76. var td = TypeDescriptor.GetConverter(typeof(T));
  77. return (T)td.ConvertFromInvariantString(@string);
  78. }
  79. public static void SetAppSetting<T>(string key, T value) where T : struct
  80. {
  81. var td = TypeDescriptor.GetConverter(typeof(T));
  82. var @string = td.ConvertToInvariantString(value);
  83. SetAppSetting(key, @string);
  84. }
  85. public static void SetAppSetting(Dictionary<string, string> settings)
  86. {
  87. if (settings == null || settings.Count == 0) return;
  88. Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  89. foreach (var setting in settings)
  90. {
  91. string key = setting.Key;
  92. string value = setting.Value;
  93. if (ConfigurationManager.AppSettings[key] == null)
  94. {
  95. configuration.AppSettings.Settings.Add(key, value);
  96. }
  97. else
  98. {
  99. configuration.AppSettings.Settings[key].Value = value;
  100. }
  101. }
  102. RefreshAndSaveSection(configuration);
  103. }
  104. public static void RefreshAndSaveSection(Configuration config)
  105. {
  106. string configPath = config.FilePath;
  107. FileInfo fileInfo = new FileInfo(configPath);
  108. fileInfo.Attributes = FileAttributes.Normal;
  109. config.Save(ConfigurationSaveMode.Modified);
  110. ConfigurationManager.RefreshSection("appSettings");
  111. }
  112. }
  113. }