app.config:如何使一个名为appSettings的嵌套customSection成为ConfigurationManager.AppSettings

时间:2011-08-02 22:33:03

标签: c# app-config appsettings configurationmanager custom-sections

我想要的app.config会是这样的:

<configSections>
        <sectionGroup name="QA_Environment">
            <section name="databases" type="System.Configuration.NameValueSectionHandler"/>
            <section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
        <sectionGroup name="Production_Environment">
            <section name="databases" type="System.Configuration.NameValueSectionHandler"/>
            <section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>

...然后我就有了实际的小组和部分。但是我会对任何有效或更好的建议感到满意。我现在已经放下了我的愿望:

    <configSections>
    <sectionGroup name="QA_Environment">
        <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <sectionGroup name="Production_Environment">
        <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
</configSections>

我猜这很好......我想知道的主要问题是我是否可以将其中一个部分替换为根级appSettings ...而无需迭代它们并以编程方式添加或创建配置并保存它。我只希望用户能够选择一个环境,select事件将改变appSettings ...

我面临的一个限制是我引用的数据层需要保持原样....所以我基本上需要让我的app.config可以像目前的那样完全访问这些其他项目......即ConfigurationManager.AppSettings [afdasdf]

如果需要澄清,请告诉我......谢谢

2 个答案:

答案 0 :(得分:4)

如果可以的话,我会继续回答我自己的问题。我发现我做得比实际要困难得多。你所要做的就是:

<?xml version="1.0" encoding="utf-8"?>

<configSections>
    <sectionGroup name="Environment">
        <sectionGroup name="QA">
            <section name="databases" type="System.Configuration.DictionarySectionHandler"/>
            <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
        </sectionGroup>
        <sectionGroup name="PROD">
            <section name="databases" type="System.Configuration.DictionarySectionHandler"/>
            <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
        </sectionGroup>
    </sectionGroup>
</configSections>

<Environment>
    <QA>
        <databases>
        </databases>
        <storageSystems>
        </storageSystems>
    </QA>

    <PROD>
        <databases>
        </databases>
        <storageSystems>
        </storageSystems>
    </PROD>
</Environment>

所以我的app.config有一部分......剩下的就是这么简单:

private void GetConfigurationSettings(TargetEnvironments targetEnvironment)
    {
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var databases = new Hashtable();
        var storageSystems = new Hashtable();

        switch (targetEnvironment)
        {
            case TargetEnvironments.QA:
                databases = (Hashtable)ConfigurationManager.GetSection("Environment/QA/databases");
                storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/QA/storageSystems");
                break;
            case TargetEnvironments.PROD:
                databases = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/databases");
                storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/storageSystems");
                break;
        }

        foreach (string key in databases.Keys) { config.AppSettings.Settings.Add(key, databases[key].ToString()); }
        foreach (string key in storageSystems.Keys) { config.AppSettings.Settings.Add(key, storageSystems[key].ToString()); }

        config.Save(ConfigurationSaveMode.Modified);

        ConfigurationManager.RefreshSection("appSettings");

        UpdateCollections();
    }

请注意,config.Save方法显然非常重要,可以立即加载您刚刚设置的设置。除此之外,它实际上只是路径名称和部分类型,我必须决定。我发现下面的链接是最有用的。如果某人有更优雅的方式,我会有兴趣听到它。

Here's the place I got the most out of in my research

答案 1 :(得分:2)

还有另一种方法可以处理特定于部署的web.config文件。您可以定义特定于部署的web.config文件,这些文件描述了对基本web.config文件的编辑命令(而不是重复所有内容)。看看this SO question的答案。

基本上,您可以定义web.debug.config和web.release.config文件,该文件在部署项目时与基本web.config文件合并。

相关问题