I want to dynamically add a custom section inside a custom sectiong group of the App.config for my .net application. I basically have 3 different environments which use the these custom sections and need to decide which one to have in my app.config file. I am currently able to edit the config file and add the custom sections and custom groups as follows :
public static void Main()
{
SetConfig configData = new SetConfig();
ConfigurationSectionGroup configSectionGroup = new ConfigurationSectionGroup();
configData.Name = "ConnectionStr";
configData.Value = "some server string";
// Write the new configuration data to the XML file
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.SectionGroups.Add("Spring", configSectionGroup);
config.Sections.Add("DatabaseConfiguration", configData);
config.Save();
ConfigurationManager.RefreshSection("DatabaseConfiguration");
Console.Write("\n");
}
Where SetConfig class has methods to enter name and value for the custom section , Now this works , but it doesnt put the < Section > inside < customGroup > . The resulting App.config file looks like this :
<configuration>
<configSections>
<section name="DatabaseConfiguration" type="System.Configuration.SetConfig, ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<sectionGroup name="Spring" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
</sectionGroup>
</configSections>
<DatabaseConfiguration name="ConnectionStr" value="some server string" />
</configuration>
Can anyone help me figure out how to fix this ? I need to add multiple sections inside the same sectionGroup at runtime.