Eitan
Eitan

Reputation: 1386

C# - override configuration manually

I am using C# .net

I need to load an external configuration file (web.config) and override the values of all sections and all elements (of each section) in current Configuration.

i.e, after the override, the state should be:

ConfigurationManager.AppSettings = ...the app settings from the web.config read...

and so on for all the sections.

In code something like (or simpler code, if any).

string s = "c:\test\web.config";

Configuration config = ConfigurationManager.OpenExeConfiguration(s);
foreach(ConfigurationSection section in config.sections) {
  // **** What to do here in order to do: ConfigurationManager.Override(section)
  // so, I can use, i.e, line code such as : string s = ConfigurationManager.AppSettings["key"] ?
}

Thanks.

Upvotes: 2

Views: 1247

Answers (2)

Eitan
Eitan

Reputation: 1386

I found a solution, and I want to participate it.

Here is the code:

// For the file name:
string s = "c:\test\web.config";

// Now creating configuration:
Configuration roamingConfig =
    ConfigurationManager.OpenExeConfiguration(s);

// configure the map
ExeConfigurationFileMap configFileMap =
    new ExeConfigurationFileMap();

configFileMap.ExeConfigFileName = s;

// Now add new configuration, based on the configuration file map.
Configuration configFile = 
    ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

var allKeys = configFile.AppSettings.Settings.AllKeys;
foreach (var key in allKeys) {
  ConfigurationManager.AppSettings["key"] = 
     configFile.AppSettings.Settings[key].value;
}

configFile.SaveAs(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("appSettings");

What I did in brief is declaring the configuration twice (the second one is open with map exe configuration), saved the file to it's native executing path, and refresh configuration section appSettings.

Upvotes: 2

MKR
MKR

Reputation: 20095

One approach could be to first overwrite config file of application using SaveAs function. And then refresh all sections of configuration using ConfigurationManager.RefreshSection function.

Option 1: With help of RefreshSection

string s = "c:\test\web.config";

var configFile = ConfigurationManager.OpenExeConfiguration(s);

//Overwrite config file of app
configFile.SaveAs(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

//Iterate through each sections 
foreach(ConfigurationSection section in configFile.Sections)
{
    //Refresh each section
    ConfigurationManager.RefreshSection(section.SectionInformation.Name);
}

[EDIT]

Option 2: Add an example to update key-value pair of AppSettings section with value read from config in different location.

string s = "c:\test\web.config";
var configFile = ConfigurationManager.OpenExeConfiguration(s);
//Get all keys
var allKeys = configFile.AppSettings.Settings.AllKeys;
foreach (var key in allKeys)
{
    // Set value in ConfigurationManager
    ConfigurationManager.AppSettings["Name"] = 
                               configFile.AppSettings.Settings[key].Value;
}

Upvotes: 1

Related Questions