ConfigManager.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Configuration;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace cuidian.Common
  11. {
  12. public static class ConfigManager
  13. {
  14. readonly static bool _Error;
  15. static Configuration _AppConfig;
  16. static ConfigManager()
  17. {
  18. string dllPath = Process.GetCurrentProcess().MainModule.FileName;
  19. // string dllPath = string.Format(
  20. // "{0}\\{1}.dll", AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory, "MyDemo.Config");
  21. try
  22. {
  23. _AppConfig = ConfigurationManager.OpenExeConfiguration(dllPath);
  24. }
  25. catch (ConfigurationErrorsException)
  26. {
  27. _Error = true;
  28. }
  29. }
  30. public static KeyValueConfigurationCollection AppSettings
  31. {
  32. get
  33. {
  34. if (_Error) return null;
  35. return _AppConfig.AppSettings.Settings;
  36. }
  37. }
  38. public static ConnectionStringSettingsCollection ConnectionStrings
  39. {
  40. get
  41. {
  42. if (_Error) return null;
  43. return _AppConfig.ConnectionStrings.ConnectionStrings;
  44. }
  45. }
  46. public static IDictionary GetSection<T>(string name) where T : ConfigurationSection
  47. {
  48. if (_Error) return null;
  49. return _AppConfig.GetSection(name) as IDictionary;
  50. }
  51. public static NameValueCollection GetSection(string name)
  52. {
  53. NameValueCollection rtn = ConfigurationManager.GetSection(name) as NameValueCollection;
  54. return rtn;
  55. }
  56. }
  57. }