AppConfigHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Runtime.Remoting;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace XdCxRhDW.Framework
  11. {
  12. public static class AppConfigHelper
  13. {
  14. public static string Get(string key, string defaultVal = "")
  15. {
  16. var str = ConfigurationManager.AppSettings[key];
  17. if (string.IsNullOrWhiteSpace(str))
  18. return defaultVal;
  19. return str.Trim();
  20. }
  21. public static string GetConnectionString(string name)
  22. {
  23. var str= ConfigurationManager.ConnectionStrings[name].ConnectionString.Trim();
  24. if (!str.EndsWith(";"))
  25. str = $"{str};";
  26. return str;
  27. }
  28. /// <summary>
  29. /// 获取App.config配置文件中appSettings节点中指定key的value
  30. /// <para>如果泛型为bool,则{"1","true","True","TRUE"}都会被转换成true,否则转换为false</para>
  31. /// <para>该方法支持int?、double?等可空类型的转换</para>
  32. /// </summary>
  33. /// <typeparam name="T"></typeparam>
  34. /// <param name="key"></param>
  35. /// <param name="defaultVal"></param>
  36. /// <returns></returns>
  37. public static T Get<T>(string key, T defaultVal = default)
  38. {
  39. var str = ConfigurationManager.AppSettings[key];
  40. if (string.IsNullOrWhiteSpace(str))
  41. {
  42. return defaultVal;
  43. }
  44. str = str.Trim();
  45. bool isNullable = typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>);
  46. if (isNullable)
  47. {
  48. NullableConverter nullableConverter = new NullableConverter(typeof(T));
  49. return (T)nullableConverter.ConvertFromString(str);
  50. }
  51. else if (typeof(T) == typeof(bool))
  52. {
  53. if (str.ToLower().Trim() == "true" || str.ToLower().Trim() == "1")
  54. {
  55. T boolVal = (T)Convert.ChangeType(true, typeof(T));
  56. return boolVal;
  57. }
  58. else
  59. {
  60. T boolVal = (T)Convert.ChangeType(false, typeof(T));
  61. return boolVal;
  62. }
  63. }
  64. else
  65. {
  66. T ret = (T)Convert.ChangeType(str, typeof(T));
  67. return ret;
  68. }
  69. }
  70. /// <summary>
  71. /// 如果参数小于0则返回0,否则返回原数字
  72. /// </summary>
  73. /// <param name="this"></param>
  74. /// <returns></returns>
  75. public static int NotLessThanZero(this int @this)
  76. {
  77. if (@this >= 0) return @this;
  78. return 0;
  79. }
  80. /// <summary>
  81. /// 为url字符串添加后缀
  82. /// </summary>
  83. /// <param name="this"></param>
  84. /// <param name="suffix"></param>
  85. /// <returns></returns>
  86. public static string AppendUrlSuffix(this string @this, string suffix)
  87. {
  88. if (suffix.StartsWith("/"))
  89. suffix = suffix.Substring(1);
  90. if (@this.EndsWith("/"))
  91. return $"{@this}{suffix}";
  92. else
  93. return $"{@this}/{suffix}";
  94. }
  95. }
  96. }