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

J B*_*min 6 c# configurationmanager app-config appsettings 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>
Run Code Online (Sandbox Code Playgroud)

...然后我就得到了正确的组和部分.但是我会对任何有效或更好的建议感到满意.我现在已经降低了我的愿望:

    <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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

J B*_*min 5

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

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

所以我的 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();
    }
Run Code Online (Sandbox Code Playgroud)

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

这是我在研究中获得最大收益的地方