using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cuidian.Common { public static class ConfigManager { readonly static bool _Error; static Configuration _AppConfig; static ConfigManager() { string dllPath = Process.GetCurrentProcess().MainModule.FileName; // string dllPath = string.Format( // "{0}\\{1}.dll", AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory, "MyDemo.Config"); try { _AppConfig = ConfigurationManager.OpenExeConfiguration(dllPath); } catch (ConfigurationErrorsException) { _Error = true; } } public static KeyValueConfigurationCollection AppSettings { get { if (_Error) return null; return _AppConfig.AppSettings.Settings; } } public static ConnectionStringSettingsCollection ConnectionStrings { get { if (_Error) return null; return _AppConfig.ConnectionStrings.ConnectionStrings; } } public static IDictionary GetSection(string name) where T : ConfigurationSection { if (_Error) return null; return _AppConfig.GetSection(name) as IDictionary; } public static NameValueCollection GetSection(string name) { NameValueCollection rtn = ConfigurationManager.GetSection(name) as NameValueCollection; return rtn; } } }