ConfigHelper.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. namespace SCC.Common
  7. {
  8. /// <summary>
  9. /// 配置管理类
  10. /// </summary>
  11. public static class ConfigHelper
  12. {
  13. /// <summary>
  14. /// 获取配置项的值
  15. /// </summary>
  16. /// <param name="key">配置项key</param>
  17. /// <returns></returns>
  18. public static object GetConfigValue(string key)
  19. {
  20. return ConfigurationManager.AppSettings[key];
  21. }
  22. /// <summary>
  23. /// 获取指定返回类型的配置项值
  24. /// 如果类型不匹配则返回该返回类型默认值
  25. /// </summary>
  26. /// <typeparam name="T">指定返回类型</typeparam>
  27. /// <param name="key">配置项key</param>
  28. /// <returns></returns>
  29. public static T GetConfigValue<T>(string key)
  30. {
  31. try
  32. {
  33. object o = ConfigurationManager.AppSettings[key];
  34. if (o != null)
  35. {
  36. return CommonHelper.ChangeType<T>(o);
  37. }
  38. }
  39. catch { }
  40. return default(T);
  41. }
  42. }
  43. }