Can't reload appSettings at runtime for different config file.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am using Visual Studio 2012 Express Edition. Here is my code:using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestMultipleConfigFiles
{
class Program
{
static void Main(string[] args)
{
object target_config_file = ConfigurationManager.AppSettings["target_config_file"];

string str_target_config_file = (target_config_file != null) ? target_config_file.ToString() : "";

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = str_target_config_file;
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");

System.Collections.Specialized.NameValueCollection appsettings = ConfigurationManager.AppSettings;

Console.WriteLine("Configuration variables:");
foreach (string str in appsettings)
{
Console.WriteLine(str);
}
}
}
}

Here is my main app.config file:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="target_config_file" value="File1.config </add>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

...and here is File1.config:<appSettings>
<add key="file1_config_key1" value="file1_config_value1 </add>
<add key="file1_config_key2" value="file1_config_value2 </add>
</appSettings>

What should happen is the additional appSettings variables in File1.config should be loaded, but they dont. The output is as follows:
Configuration variables:
target_config_file

Press any key to continue . . .
The output should have "file1_config_key1" and "file1_config_key2." What am I doing wrong?

View the full article
 
Back
Top