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

本文关键字:appSettings customSection Configurati 成为 嵌套 config 何使一 app | 更新日期: 2023-09-27 18:04:32

我想要的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]

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

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

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

<?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();
    }

请注意配置的明显重要用途。方法,以立即加载刚刚设置的设置。除此之外,我只需要决定路径名和section类型。我发现下面的链接是最有用的。如果有人有更优雅的方法,我很想听听。

这是我在研究中收获最多的地方

还有另一种处理特定于部署的web的方法。配置文件。您可以定义特定于部署的web。向基础web描述编辑命令的配置文件。配置文件(而不是重复所有内容)。请看下面这个问题的答案。

基本上,你可以定义一个web.debug.config和一个web.release.config文件与基本web合并。