C#项目

时间:2015-09-17 14:31:55

标签: c# app-config

我有两个项目:UI(Winforms应用程序)和Core(dll)。 每个项目都必须拥有其配置文件。

例如:

核心项目(settings.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="environment" value="development"/>
    </appSettings>
</configuration>

UI项目(app.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="skin" value="win"/>
    </appSettings>
</configuration>

从Core我将settings.config读作:

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;

此时崩溃并抛弃我无法将System.Configuration.DefaultSection投射到System.Configuration.AppSettingsSection

1 个答案:

答案 0 :(得分:0)

我认为你需要的是这个

        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;