static ConfigManager()
 {
     _internalCache = new Hashtable(10);
     ConfigurationManagementSettings cms = new ConfigurationManagementSettings();
     _appConfigFile = cms.GetAppConfigFile();
     Initialize();
     _isInitialized = true;
 }
        /// <summary>
        /// Reads a section from the config.
        /// </summary>
        /// <param name="sectionName"> The configuration section name.</param>
        /// <param name="caller"> The type calling this method. This type is use for config file location.</param>
        /// <param name="useCache"> Sets the ConfigManager to save the configuration object in the cache store.</param>
        /// <returns> The configuration object.</returns>		
        public static object Read(string sectionName, Type caller, bool useCache)
        {
            ConfigurationManagementSettings cms = new ConfigurationManagementSettings();
            _appConfigFile = cms.GetAppConfigFile(caller.Assembly.Location);
            Initialize();
            _isInitialized = true;

            if ( useCache )
            {
                // Cache
                if ( _internalCache[sectionName] == null )
                {
                    _internalCache.Add(sectionName,Read(sectionName));
                }

                return _internalCache[sectionName];
            }
            else
            {
                // no cache
                return Read(sectionName);
            }
        }
        /// <summary>
        /// Loads a configuration section from a configuration file or XML.
        /// </summary>
        /// <param name="sectionName"> The section name to load.</param>
        /// <param name="fileName"> The file name of the configuration or XML. Leave empty to use default configuration.</param>
        public virtual object Load(string sectionName, string fileName)
        {
            string file;
            if ( fileName.Length == 0 )
            {
                ConfigurationManagementSettings cms = new ConfigurationManagementSettings();
                file = cms.GetAppConfigFile();
            }
            else
            {
                file = fileName;
            }

            // Read
            XmlNode sectionNode = ConfigurationManagementSettings.ReadConfigNode(sectionName, file);
            object result;

            // Serialize
            if ( sectionNode == null )
            {
                result = null;
            }
            else
            {
                result = this.Create(null, null, sectionNode);
            }

            return result;
        }
        /// <summary>
        /// Saves the configuration.
        /// </summary>
        /// <param name="value"> The object to save.</param>
        /// <param name="sectionName"> The section name to save to.</param>
        /// <param name="fileName"> The file name of the configuration or XML. Leave empty to use default configuration.</param>
        public virtual void Save(object value, string sectionName, string fileName)
        {
            string file;
            if ( fileName.Length == 0 )
            {
                ConfigurationManagementSettings cms = new ConfigurationManagementSettings();
                file = cms.GetAppConfigFile();
            }
            else
            {
                file = fileName;
            }

            // Serialize
            XmlNode node = this.Serialize(value);

            // Write
            ConfigurationManagementSettings.WriteConfigNode(sectionName, node, file);
        }
        private static void Initialize()
        {
            try
            {
                FileStream stream = new FileStream(_appConfigFile,FileMode.Open, FileAccess.Read);

                // open config file
                XPathDocument doc = new XPathDocument(new XmlTextReader(stream));
                XPathNavigator navigator = doc.CreateNavigator();

                // Nodes
                XPathExpression expr = navigator.Compile("configuration/configSections");
                XPathNodeIterator nodes = navigator.Select(expr);

                if ( nodes.Count > 0 )
                {
                    nodes.MoveNext();

                    // Load Section Handlers
                    ConfigurationManagementSettings settings = new ConfigurationManagementSettings();
                    settings.LoadSectionHandlers(nodes.Current);
                    _handlers = settings.SectionHandlers;
                }

                stream.Close();
            }
            catch ( Exception ex )
            {
                throw new ConfigurationException("Configuration settings error.",ex);
            }
        }