gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:36:04 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com