Initialize a configuration section
in the configuration file, not config section is defined, you can use the following code to initialize the configuration.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); MyConfigSection sec = fred.GetSection("MyConfigSection") as MyConfigSection; if (sec == null) { Console.WriteLine("sec is null"); sec = new MyConfigSection(); sec.SectionInformation.AllowDefinition = ConfigurationAllowDefinition.MachineToApplication; sec.Age = 12; config.Sections.Add("mysec", sec); config.Save(ConfigurationSaveMode.Minimal); }
<p>
Please note that the sec should be also initialized, if there is not, only the Section definiton is defined, but not the actual configuration section.
</p>