Deals with Loading Configuration values from the config file.
Inheritance: System.Configuration.ConfigurationSection, IDisposable
Exemplo n.º 1
0
        /// <summary>
        /// creates the configurationLoader for the given path.
        /// </summary>
        /// <param name="pathToConfigFile"> </param>
        /// <param name="configPath">path to the configuration section in the config file.</param>
        /// <param name="allowValueInheritance">should all child configsection's elements inherit from their parent?</param>
        /// <returns>If the configuration Loader was created successfully, doesn't gaurantee items have been found, only that it is not null.</returns>
        private bool CreateConfigurationLoaderObject(string pathToConfigFile, string configPath, bool allowValueInheritance)
        {
            if (string.IsNullOrEmpty(configPath))
            {
                throw new ArgumentException(configPath);
            }

            try
            {
                if (string.IsNullOrEmpty(pathToConfigFile.Trim()) || !File.Exists(pathToConfigFile))
                {
                    ConfigSectionLoader = ConfigurationManager.GetSection(configPath) as ConfigurationSectionLoader;
                }
                else
                {
                    var fileMap       = new ConfigurationFileMap(pathToConfigFile); //Path to your config file
                    var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
                    ConfigSectionLoader = configuration.GetSection(configPath) as ConfigurationSectionLoader;
                }

                if (ConfigSectionLoader == null)
                {
                    return(false);
                }

                foreach (ConfigurationGroupElement configGroup in ConfigSectionLoader.ConfigGroups)
                {
                    ConfigSectionNames.Add(configGroup.Name);
                }

                SetIndexesForConfigGroupCollectionRecursively(ConfigSectionLoader.ConfigGroups);
            }
            catch (ConfigurationErrorsException)
            {
                //error with configuration inside what seems to be a valid section, rethrow.
                throw;
            }
            catch (Exception ex)
            {
                //no good, try more options.
                return(false);
            }
            return(ConfigSectionLoader != null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// creates the configurationLoader for the given path.
        /// </summary>
        /// <param name="pathToConfigFile"> </param>
        /// <param name="configPath">path to the configuration section in the config file.</param>
        /// <param name="allowValueInheritance">should all child configsection's elements inherit from their parent?</param>
        /// <returns>If the configuration Loader was created successfully, doesn't gaurantee items have been found, only that it is not null.</returns>
        private bool CreateConfigurationLoaderObject(string pathToConfigFile, string configPath, bool allowValueInheritance)
        {
            if (string.IsNullOrEmpty(configPath))
            {
                throw new ArgumentException(configPath);
            }

            try
            {
                if (string.IsNullOrEmpty(pathToConfigFile.Trim()) || !File.Exists(pathToConfigFile))
                {
                    ConfigSectionLoader = ConfigurationManager.GetSection(configPath) as ConfigurationSectionLoader;
                }
                else
                {
                    var fileMap = new ConfigurationFileMap(pathToConfigFile); //Path to your config file
                    var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
                    ConfigSectionLoader = configuration.GetSection(configPath) as ConfigurationSectionLoader;
                }

                if (ConfigSectionLoader == null) return false;

                foreach (ConfigurationGroupElement configGroup in ConfigSectionLoader.ConfigGroups)
                {
                    ConfigSectionNames.Add(configGroup.Name);
                }

                SetIndexesForConfigGroupCollectionRecursively(ConfigSectionLoader.ConfigGroups);
            }
            catch (ConfigurationErrorsException)
            {
                //error with configuration inside what seems to be a valid section, rethrow.
                throw;
            }
            catch (Exception ex)
            {
                //no good, try more options.
                return false;
            }
            return ConfigSectionLoader != null;
        }