There are several questions about reading custom config files but none of them address using custom config sections. Here is a section from my app.config:
<configuration>
<configSections>
<section name="DatabaseConfigSection" type="Bootstrapper.DatabaseConfig, Bootstrapper" />
</configSections>
<DatabaseConfigSection>
<Configuration>
<Databases>
<Database name="Database_Dev" environment="Dev"/>
<Database name="Database_RC" environment="RC"/>
<Database name="Database_Cloud" environment=""/>
</Databases>
</Configuration>
</DatabaseConfigSection>
</configuration>
And I have a custom config class that allows me to code against it like this:
DatabaseConfig.Instance.Configuration.Databases.Cast<Database>().Select(x => x.Name).ToArray();
I want to have another, optional config file with the same structure as my app.config file above and I want to use my custom config class against it. For example, something like this:
ConfigurationManager.Use("otherConfigFile.config");
var new dbConfig = ConfigurationManager.GetSection(SectionSectionName) as DatabaseConfig;
dbConfig.Databases.Cast<Database>().Select(x => x.Name).ToArray();
And have it return the data from "otherConfigFile.config" as a DatabaseConfig
object. However, I can't find a way to make ConfigurationManager.GetSection()
point to a different file. Thanks!
Here's what I ended up doing:
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"C:\Users\sirdank\project\bootstrapper\bin\Debug\test.config";
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
DatabaseConfig dbConfig = config.GetSection("DatabaseConfigSection") as DatabaseConfig;
Put together with the help of MSDN