my desired app.config would be like this:
<configSections>
<sectionGroup name="QA_Environment">
<section name="databases" type="System.Configuration.NameValueSectionHandler"/>
<section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
<sectionGroup name="Production_Environment">
<section name="databases" type="System.Configuration.NameValueSectionHandler"/>
<section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
...and then I've got the actual groups and sections right below that. But I'd be happy with whatever works or better suggestions though. I've now lowered my wishes to this:
<configSections>
<sectionGroup name="QA_Environment">
<section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
<sectionGroup name="Production_Environment">
<section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
And I guess that's fine...The main thing I'm wondering about is if I can substitute one of these sections as the root level appSettings...without iterating through them and programmatically adding or creating the config and saving it. I just want the user to be able to select an environment, the select event will change the appSettings...
One constraint that I'm facing is that the data layer I'm referencing needs to remain the same as it is....so I basically need to get my app.config to be accessible exactly like it is currently from these other projects...that is ConfigurationManager.AppSettings[afdasdf]
Let me know if this needs any clarification...thanks
Store custom information from a configuration file - C#
The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.
Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.
I'll go ahead and answer my own question here if that's alright. I found that I was making it much harder than it really is. All you have to do is this:
<?xml version="1.0" encoding="utf-8"?>
<configSections>
<sectionGroup name="Environment">
<sectionGroup name="QA">
<section name="databases" type="System.Configuration.DictionarySectionHandler"/>
<section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
</sectionGroup>
<sectionGroup name="PROD">
<section name="databases" type="System.Configuration.DictionarySectionHandler"/>
<section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
</sectionGroup>
</sectionGroup>
</configSections>
<Environment>
<QA>
<databases>
</databases>
<storageSystems>
</storageSystems>
</QA>
<PROD>
<databases>
</databases>
<storageSystems>
</storageSystems>
</PROD>
</Environment>
So there's part of my app.config....the rest is just as easy:
private void GetConfigurationSettings(TargetEnvironments targetEnvironment)
{
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var databases = new Hashtable();
var storageSystems = new Hashtable();
switch (targetEnvironment)
{
case TargetEnvironments.QA:
databases = (Hashtable)ConfigurationManager.GetSection("Environment/QA/databases");
storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/QA/storageSystems");
break;
case TargetEnvironments.PROD:
databases = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/databases");
storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/storageSystems");
break;
}
foreach (string key in databases.Keys) { config.AppSettings.Settings.Add(key, databases[key].ToString()); }
foreach (string key in storageSystems.Keys) { config.AppSettings.Settings.Add(key, storageSystems[key].ToString()); }
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
UpdateCollections();
}
Note the obviously important use of the config.Save method to immediately load the settings that you've just set. Other than that, it's really just the path names and the section type that I had to kinda decide on. I found the link below to be the most useful. If someone's got a more elegant way I'd be interested to hear about it.
Here's the place I got the most out of in my research
There is another way to deal with deployment-specific web.config files. You can define deployment-specific web.config files that describe edit commands to the base web.config file (rather than repeating everything). Have a look at the answers for this SO question.
Basically, you can define a web.debug.config and a web.release.config file that gets merged with the base web.config file when you deploy your projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With