App.config: how to make a nested customSection called appSettings will be ConfigurationManager.AppSettings

my desired app.config would look 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 have the actual groups and sections right below that. But I would be happy with any jobs or best offers. Now I have omitted my wishes:

  <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 think it's fine ... The main thing I'm interested in is that I can replace one of these sections with the root level of appSettings ... without repeating through them and programmatically add or create a configuration and save it, I just want so that the user can select the environment, the select event will change appSettings ...

One limitation that I am facing is that the data layer that I refer to must remain the same as it .... therefore I basically need my app.config to be accessible in exactly the same way as these other projects are now ... this is ConfigurationManager.AppSettings [afdasdf]

Let me know if this requires any clarification ... thanks

+6
source share
2 answers

I will go further and answer my question here if this is good. I found that I am doing this a lot harder than it really is. All you have to do is the following:

 <?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 part of my app.config ... the rest is just as simple:

 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 clearly important use of the config.Save method to immediately load the settings you just set. Other than that, these are really just the path names and type of section that I had to solve. I found the link below to be most useful. If someone got a more elegant way, I would be interested to hear about it.

Here is the place where I got the most from my research

+4
source

There is another way to work with deployment-specific web.config files. You can define web.config files for a specific deployment that describe editing commands to the underlying web.config file (rather than repeating everything). See the answers to this SO question .

Basically, you can define the web.debug.config file and the web.release.config file that will be merged with the base web.config file when you deploy your projects.

+2
source

Source: https://habr.com/ru/post/894152/


All Articles