c# - app.config : how do I make a nested customSection called appSettings be the ConfigurationManager. 应用设置

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

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

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

最佳答案

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

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

关于c# - app.config : how do I make a nested customSection called appSettings be the ConfigurationManager. 应用设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919510/

相关文章:

visual-studio - Visual Studio 和子版本 : What about Machine Specific Files

c# - 无法从 App.config 引用变量

.net-core - azure webjob : Read appSettings. json 并将配置注入(inject) TimerTrigger

C# 向文本文件添加行号

C# 异步 WebRequest 与 POST

configuration - 有什么方法可以将部分添加到不会被 ConfigurationManager 解析的 .config 文件中

c# - (新格式)visual studio 项目中的可选 appsettings.local.json

c# - NLog 连接字符串的 appsettings.json 中的引用变量

c# - 使用自己的 app.config 的插件

c# - 在 C# 中访问消息元素的内部嵌套 JSON 数组