I've two projects: UI (Winforms application) and Core (dll). Each project has to own its config file.
For example:
Core project (settings.config)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="environment" value="development"/>
</appSettings>
</configuration>
UI project (app.config)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="skin" value="win"/>
</appSettings>
</configuration>
From Core I read settings.config as:
System.Configuration.ConfigurationFileMap fileMap = new System.Configuration.ConfigurationFileMap("settings.config");
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string environment = configuration.AppSettings.Settings["environment"].Value;
At this point crashes and dumps me that it's unable to cast System.Configuration.DefaultSection
to System.Configuration.AppSettingsSection
.
I think what you need is this
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = "settings.config"
};
var configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,
ConfigurationUserLevel.None);
var section = (AppSettingsSection)configuration.GetSection("appSettings");
string value = section.Settings["environment"].Value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.