【问题标题】:app.config: how do I make a nested customSection called appSettings be the ConfigurationManager.AppSettingsapp.config:如何使名为 appSettings 的嵌套 customSection 成为 ConfigurationManager.AppSettings
【发布时间】:2011-10-18 16:05:09
【问题描述】:

我想要的 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...无需遍历它们并以编程方式添加或创建配置并保存它.我只是希望用户能够选择一个环境,选择事件会改变 appSettings...

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

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

【问题讨论】:

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


    【解决方案1】:

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

    <?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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-05
        • 2014-08-11
        • 1970-01-01
        • 2011-12-12
        • 2022-01-22
        • 1970-01-01
        • 2021-09-27
        相关资源
        最近更新 更多