ConfigHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Configuration;
  3. namespace Ant.Service.Utilities
  4. {
  5. /// <summary>
  6. /// web.config操作类
  7. /// </summary>
  8. public sealed class ConfigHelper
  9. {
  10. /// <summary>
  11. /// 得到AppSettings中的配置字符串信息
  12. /// </summary>
  13. /// <param name="key"></param>
  14. /// <returns></returns>
  15. public static string GetConfigString(string key)
  16. {
  17. string CacheKey = "AppSettings-" + key;
  18. object objModel = DataCache.GetCache(CacheKey);
  19. if (objModel == null)
  20. {
  21. try
  22. {
  23. objModel = ConfigurationManager.AppSettings[key];
  24. if (objModel != null)
  25. {
  26. DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
  27. }
  28. }
  29. catch
  30. { }
  31. }
  32. return objModel.ToString();
  33. }
  34. /// <summary>
  35. /// 得到AppSettings中的配置Bool信息
  36. /// </summary>
  37. /// <param name="key"></param>
  38. /// <returns></returns>
  39. public static bool GetConfigBool(string key)
  40. {
  41. bool result = false;
  42. string cfgVal = GetConfigString(key);
  43. if(null != cfgVal && string.Empty != cfgVal)
  44. {
  45. try
  46. {
  47. result = bool.Parse(cfgVal);
  48. }
  49. catch(FormatException)
  50. {
  51. // Ignore format exceptions.
  52. }
  53. }
  54. return result;
  55. }
  56. /// <summary>
  57. /// 得到AppSettings中的配置Decimal信息
  58. /// </summary>
  59. /// <param name="key"></param>
  60. /// <returns></returns>
  61. public static decimal GetConfigDecimal(string key)
  62. {
  63. decimal result = 0;
  64. string cfgVal = GetConfigString(key);
  65. if(null != cfgVal && string.Empty != cfgVal)
  66. {
  67. try
  68. {
  69. result = decimal.Parse(cfgVal);
  70. }
  71. catch(FormatException)
  72. {
  73. // Ignore format exceptions.
  74. }
  75. }
  76. return result;
  77. }
  78. /// <summary>
  79. /// 得到AppSettings中的配置int信息
  80. /// </summary>
  81. /// <param name="key"></param>
  82. /// <returns></returns>
  83. public static int GetConfigInt(string key)
  84. {
  85. int result = 0;
  86. string cfgVal = GetConfigString(key);
  87. if(null != cfgVal && string.Empty != cfgVal)
  88. {
  89. try
  90. {
  91. result = int.Parse(cfgVal);
  92. }
  93. catch(FormatException)
  94. {
  95. // Ignore format exceptions.
  96. }
  97. }
  98. return result;
  99. }
  100. }
  101. }