There are questions pertaining to reading settings from a separate config file and others similar to it, but my question is specific to application property settings (i.e. <MyApplication.Properties.Settings>
- see XML file below) and how to load them dynamically. I tried the method in this post, which involved refreshing the entire appSettings section of the main config file, but my adaptation threw exceptions because I wasn't replacing the appSettings section:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
// Have tried the other ConfigurationUserLevels to no avail
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified); // throws ConfigurationErrorsException
ConfigurationManager.RefreshSection("userSettings");
The ConfigurationErrorsException.Message is "The root element must match the name of the section referencing the file, 'appSettings' (C:\myFile.xml line 2)." The file is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<MyApplication.Properties.Settings>
<setting name="SineWaveFrequency" serializeAs="String">
<value>6</value>
</setting>
<setting name="SineWaveAmplitude" serializeAs="String">
<value>6</value>
</setting>
</MyApplication.Properties.Settings>
</userSettings>
</configuration>
Is there a way to import the values from this file into the MyApplication.Properties.Settings.Default
class, with the framework handling all XML deserialization like it does when the config file is loaded on application startup?