The Config class is a wrapper around the ConfigurationSectionLoader. It deals with creating and loading in the variables. It allows easier access to any Value for a given key. For a given key it will return NULL or the value if found.
Inheritance: IDisposable
        public SettingsModel GetSettinngs()
        {
            if (_settings == null)
            {
                _settings = new SettingsModel();

                var config = new Config().GetSection("settings");
                if (config.Collections.ContainsKey("EwsSettings"))
                {
                    _settings.EwsSettings = config.Collections.GetCollection("EwsSettings").Create<EwsSettings>();
                }

                if (config.Collections.ContainsKey("TrelloSettings"))
                {
                    _settings.TrelloSettings = config.Collections.GetCollection("TrelloSettings").Create<TrelloSettings>();
                }

                if (config.Collections.ContainsKey("SlackSettings"))
                {
                    _settings.SlackSettings = config.Collections.GetCollection("SlackSettings").Create<SlackSettings>();
                }
            }

            return _settings;
        }
        public static DomainModel LoadDomainModelTemplateWithSettings()
        {
            ConfigSection configSection = new Config(@"MoreComplexExamples\AutoGenerateModelsFromConfigWithDefaultValues\autogendefaults.config", "domainModelTemplate").GetSection("model");
            ObjectCreationSettingsCollection creationSettings = configSection.CreateCreationSettingsCollection(false); //set private properties as well.
            creationSettings.SetValue("NumberUnits", "123");

            return configSection.Create<DomainModel>(creationSettings);
        }
        public LoadingAConfigFileThatIsNotTheDefaultConfig()
        {
            ConfigSection configSection = new Config("app.Testfile.config", "testsection5").GetSection("clientb");
            string myVal = configSection["key23"];

            Console.WriteLine("configloader loaded client section called 'clientb'");
            Console.WriteLine("value found for 'key23' is: " + myVal);

            Console.WriteLine("");
        }
        /// <summary>
        /// The loading of the config object did not specify a config path so the application determined the first valid path it could find.
        /// below is the configSections segment of the app config file.  It chooses to use "testsection2" becuase it appears first in the list.
        /// If this had been declared after the sectiongroup element or not at all then it would have choosen the section group "myCustomGroup".
        /// 
        /// configSections
        ///     section name="testsection2" type="CustomConfigurations.ConfigurationSectionLoader, CustomConfigurations"
        ///     sectionGroup name="myCustomGroup">
        ///         section name="mysection" type="CustomConfigurations.ConfigurationSectionLoader, CustomConfigurations"
        ///         sectionGroup
        /// configSections
        /// 
        /// </summary>
        public LoadingOfConfigurationByAllowingAppToDetermineConfigPath()
        {
            ConfigSection configSection = new Config().GetSection("clienta");
            string myVal = configSection["key2"];

            Console.WriteLine("configloader loaded client section called 'clienta'");
            Console.WriteLine("value found for 'key' is: " + myVal);

            Console.WriteLine("");
        }
        /// <summary>
        /// Loading the config object can be done by providing only a section name rather than providing the whole config path of sectiongroup and sectionname.
        /// </summary>
        public LoadingByProvidingSectionNameOnly()
        {
            ConfigSection configSection = new Config("testsection2").GetSection("clienta");
            string myVal = configSection["key2"];

            Console.WriteLine("configloader loaded client section called 'clienta'");
            Console.WriteLine("value found for 'key' is: " + myVal);

            Console.WriteLine("");
        }
        public static DomainModel LoadDomainModelTemplate()
        {
            ConfigSection configSection = new Config(@"MoreComplexExamples\AutoGenWithNonEmptyConstructor\autogenctor.config", "domainModelTemplate").GetSection("model");

            ConfigValueDictionary mappings = new ConfigValueDictionary();
            mappings.Add("mySecretNum", "MySecretNumber");  //pass in the param name for the constructor and the key of the corresponding ValueItem from config
            mappings.Add("numberUnits", "NumberUnits");    //remember that all params are case sensitive

            //if mappings had not been used it would not have known what values to use for both of these values which it got from config
            return configSection.Create<DomainModel>(mappings, true);
        }
        public static IList<ComponentGraph> LoadInstallerComponentGraphs()
        {
            Config deployments = new Config(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            if (deployments == null)
            {
                throw new ApplicationException("could not load configuraiton");
            }
            List<string> sectionNames = deployments.SectionNames.ToList();
            if (sectionNames == null || !sectionNames.Any())
            {
                throw new ApplicationException("no sections deployments were found in the configuration file.");
            }

            if (sectionNames.Count() != deployments.Count)
            {
                throw new ArgumentException("error loading configuration, number of deployments vs sections name differ");
            }

            IList<ComponentGraph> componentGraphs = new List<ComponentGraph>(deployments.Count);

            for (int i = 0; i < deployments.Count; i++)
            {
                ComponentGraph component = new ComponentGraph();
                string deploymentName = sectionNames[i];
                Console.WriteLine("Reading Configuration group '{0}'", deploymentName);

                ConfigSection deployment = deployments.GetSection(deploymentName);
                if (deployment == null)
                {
                    Console.WriteLine("Error reading configuraiton group '{0}'", deploymentName);
                    continue;
                }

                IDictionary<string, string> globalValues = deployment.ValuesAsDictionary;

                component.Index = deployment.Index; //set index of all items for sorting later.
                List<ConfigSection> deploymentConfigCollections = deployment.Collections.GetCollections().ToList();
                for (int index = 0; index < deploymentConfigCollections.Count(); index++)
                {
                    ConfigSection installerSection = deploymentConfigCollections[index];
                    Console.WriteLine("Reading Configuration component section '{0}'", installerSection.Name);
                    var item = ComponentFactory.Create(globalValues, installerSection, index);
                    if (item != null)
                    {
                        component.Components.Add(item);
                    }
                }

                componentGraphs.Add(component);
            }

            return componentGraphs;
        }
        public static DeploymentStrategyComponentGraphBase Load(string deploymentName, string configPath, bool? forceLocal)
        {
            Config deployments = new Config(configPath);
            if(!deployments.SectionNames.Contains(deploymentName))
            {
                throw new ArgumentOutOfRangeException("deploymentName");
            }

            //got the deployment we want to create / setup
            ConfigSection deployment = deployments.GetSection(deploymentName);
            if (!deployment.ContainsKey("DestinationComputerName"))
            {
                throw new ArgumentOutOfRangeException("DestinationComputerName");
            }

            string destination = deployment["DestinationComputerName"];
            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ArgumentException("DestinationComputerName is empty");
            }

            DeploymentStrategyComponentGraphBase deploymentStrategyComponentGraph;

            if (destination.ToLower().Contains("localhost") || (forceLocal.HasValue && forceLocal.Value))
            {
                deploymentStrategyComponentGraph = deployment.Create<LocalDeploymentStrategyComponentGraphBase>();
            }
            else
            {
                deploymentStrategyComponentGraph = deployment.Create<RemoteDeploymentStrategyComponentGraphBase>();
            }

            if (!deployment.ContainsSubCollections)
            {
                return deploymentStrategyComponentGraph;
            }

            //create the actions for the deployment.
            CreateActions(deploymentStrategyComponentGraph, deployment, GetMsdeployLocations(deployment));

            //update the username and password if given.
            var remoteDetails = GetRemoteDetails(deployment);
            if (remoteDetails != null)
            {
                foreach (var action in deploymentStrategyComponentGraph.Actions)
                {
                    action.DestinationUserName = remoteDetails.DestinationUserName;
                    action.DestinationPassword = remoteDetails.DestinationPassword;
                    action.AuthType = remoteDetails.AuthType;
                }
            }

            return deploymentStrategyComponentGraph;
        }
        public static IList<Task> LoadTasks()
        {
            ConfigSection section = new Config(@"MoreComplexExamples\ValueInheritance\ValueInheritance.config", "domainModelTemplate").GetSection("model");

            IList<Task> Tasks = new List<Task>();
            foreach (ConfigSection childSection in section.Collections.GetCollections())
            {
                Tasks.Add(childSection.Create<Task>());
            }

            return Tasks;
        }
        public SimpleLoadingOfConfiguration()
        {
            Config configLoader = new Config("client2");
            string myVal = configLoader["keya"];
            bool result;
            int myInt = configLoader.TryParse<int>("key2", out result);

            Console.WriteLine("configloader loaded client section called 'client2'");
            Console.WriteLine("value found for 'key' is: " + myVal);
            Console.WriteLine(string.Format("attempted try parse for 'key2' to convert to int result '{0}', value {1}", result, myInt));

            Console.WriteLine("");
        }
        /// <summary>
        /// Loading a specified Section by giving the path to the custom config and the section name.
        /// </summary>
        public SimpleLoadingOfConfiguration()
        {
            ConfigSection configSection = new Config("myCustomGroup/mysection").GetSection("client1");
            string myVal = configSection["key2"];
            bool result;
            int myInt = configSection.TryParse<int>("key5", out result);

            Console.WriteLine("configloader loaded client section called 'client2'");
            Console.WriteLine("value found for 'key2' is: " + myVal);
            Console.WriteLine(string.Format("attempted try parse for 'key5' to convert to int result '{0}', value {1}", result, myInt));

            Console.WriteLine("");
        }
        public CreatingStronglyTypedObjectsFromConfig()
        {
            ConfigSection configSection = new Config("myCustomGroup/mysection").GetSection("client1");

            MyObject myObj = new MyObject();

            myObj.Name = configSection.Name;
            myObj.Prop1 = configSection["key2"];
            myObj.Prop2 = configSection["key3"];
            myObj.Prop3 = configSection["key4"];

            Console.WriteLine("created a strongly typed object with string values of from config:");
            Console.WriteLine(myObj);

            Console.WriteLine("");
        }
        /// <summary>
        /// Example of using the Collections and Collection elements, these can be nested as deep as required, 
        /// (n.b if doing deep nesting might want to consider if the current approach is the best fit).
        /// </summary>
        public UsingInnerCollections()
        {
            ConfigSection configSection = new Config("nestedCollections").GetSection("client1");

            ConfigSection innerCollection = configSection.Collections.GetCollection("col2");
            string myVal = innerCollection["key4"];

            ConfigSection nestedCollection = innerCollection.Collections.GetCollection("col3");
            string myNestedVal = nestedCollection["key2a"];

            Console.WriteLine("configloader loaded client section called 'client1'");
            Console.WriteLine("There is an inner collection called 'col2' which has a value for key: 'key4' of: " + myVal);
            Console.WriteLine("There is also a nested collection inside the inner collection called 'col3' with a value for key: 'key2a' of: " + myNestedVal);
            Console.WriteLine("The nesting of collections does not have a physical or logical limit, but deep nesting might be a sign that a rethink could be needed.");

            Console.WriteLine("");
        }
        public static DomainModel LoadDomainModelTemplate()
        {
            ConfigSection configSection = new Config(@"MoreComplexExamples\WrappingConfigurationIntoALoader\wrapping.config", "domainModelTemplate").GetSection("model");

            DomainModel model = new DomainModel();
            model.Name = configSection.Name;

            bool canExecute;
            model.CanExecute = bool.TryParse(configSection["ShouldExecute"], out canExecute) && canExecute;

            if (!string.IsNullOrEmpty(configSection["description"]))
            {
                model.Description = configSection["description"];
            }

            int noUnits;
            model.NumberUnits = Int32.TryParse(configSection["noUnits"], out noUnits) ? noUnits : 0;

            try
            {
                DomainModelType modelType = (DomainModelType)Enum.Parse(typeof(DomainModelType), configSection["domainType"]);
                model.ModelType = modelType;
            }
            catch
            {
                model.ModelType = DomainModelType.MyType;
            }

            if (configSection.ContainsSubCollections)
            {
                if (configSection.Collections.ContainsKey("Contacts"))
                {
                    ConfigSection contacts = configSection.Collections.GetCollection("Contacts");
                    foreach (string contactName in contacts.ValuesAsDictionary.Values)
                    {
                        model.Contacts.Add(new DomainContact { Name = contactName });
                    }
                }
            }

            return model;
        }
        /// <summary>
        /// Loads the config object and gets two different configSections to be able to get variables key values across both sections.
        /// </summary>
        public LoadingMultipleConfigSections()
        {
            Config config = new Config("myCustomGroup/mysection");

            foreach (var sectionName in config.SectionNames)
            {
                Console.WriteLine("section name key found: " + sectionName);
            }

            ConfigSection configSection1 = config.GetSection("client1");
            ConfigSection configSection2 = config.GetSection("client2");

            string myVal1 = configSection1["key2"];
            string myVal2 = configSection2["key2"];

            Console.WriteLine("loaded two client objects, client1 and client2");
            Console.WriteLine("found values for both, each using the same key, ('key2')");
            Console.WriteLine("client 1 key val: " + myVal1);
            Console.WriteLine("client 2 key val: " + myVal2);
            Console.WriteLine("");
        }
 public static void CreateEncryptionKey()
 {
     var config = new Config();
     config.CreateConfigKey();
 }
 public static void EncryptDeployments()
 {
     var config = new Config();
     config.EncryptConfigurationSection("deployments");
 }