WARNING : configSource is not the correct answer as indicated in other answers.
Now for the bad news ... none of the external files will be saved if you update the value read using the ConfigurationManager.
Setup: Command Line Project App.config File:
<appSettings file="AppSettings.config"> <add key="test" value="MAIN" /> </appSettings>
AppSettings.config file with "Copy to output directory" = "Always copy"
<?xml version="1.0" encoding="utf-8"?> <appSettings> <add key="test" value="OVERRIDDEN"/> </appSettings>
Program.cs:
Console.WriteLine("Local Config sections"); try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("press the ENTER key to end"); Console.ReadLine();
TEST1 - appsettings settings overridden by configSource sub-config
Sort:
Change app.config to use 'configSource'
<appSettings configSource="AppSettings.config">
Act: RunProject
Assert: Exception Raised: "Partitions should appear only once in the configuration file."
TEST2 - appsettings settings overridden by configSource sub-config
Sort:
Change app.config to use 'file'
<appSettings file="AppSettings.config">
Act: RunProject
Approve:
config.AppSettings.Settings ["test"]. Value = OVERRIDDEN
config.AppSettings.Settings ["testExternalOnly"]. Value = INITIALSTATE
Now for the bad news ... none of the external files will be saved if you use
config.AppSettings.Settings["test"].Value = "NEW";
AppSettings will only be saved in the .exe.config file, and then, if you close and open your configuration, the values ββwill be overwritten by the external values ββof the file.
TEST3 . Values ββin external files are not updated in the "config.Save" section
Setting: Program.cs changed to:
Console.WriteLine("Local Config sections"); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("BEFORE[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("BEFORE[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); config.AppSettings.Settings["test"].Value = "NEW"; config.AppSettings.Settings["testExternalOnly"].Value = "NEWEXTERNAL"; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); //Shut current config config = null; //Open config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); Console.WriteLine("AFTER[test]=" + config.AppSettings.Settings["test"].Value); Console.WriteLine("AFTER[testExternalOnly]=" + config.AppSettings.Settings["testExternalOnly"].Value); Console.WriteLine("AFTER CONFIG EXTERNAL FILE: " + System.IO.File.ReadAllText("AppSettings.config")); Console.WriteLine("AFTER CONFIG FILE: " + System.IO.File.ReadAllText(System.AppDomain.CurrentDomain.FriendlyName + ".config"));
Act: Run the program
Approve:
{myExe} .exe.confg AppSettings section:
<appSettings file="AppSettings.config"> <add key="test" value="NEW" /> <add key="testExternalOnly" value="NEWEXTERNAL" /> </appSettings>
AppSettings.config does not change