C# app.config:如何将名为appSettings的嵌套customSection设置为ConfigurationManager.appSettings
我想要的app.config如下:C# app.config:如何将名为appSettings的嵌套customSection设置为ConfigurationManager.appSettings,c#,app-config,appsettings,configurationmanager,custom-sections,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.C
<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…而无需迭代它们,并以编程方式添加或创建配置并保存它。我只希望用户能够选择一个环境,选择事件将更改应用程序设置
我面临的一个约束是,我引用的数据层需要保持不变……因此我基本上需要让我的app.config能够像当前从这些其他项目中访问一样进行访问……这就是ConfigurationManager.AppSettings[afdasdf]
如果需要澄清,请告诉我……谢谢有另一种方法可以处理特定于部署的web.config文件。您可以定义特定于部署的web.config文件,这些文件描述对基本web.config文件的编辑命令(而不是重复所有操作)。看看你的答案
基本上,您可以定义一个web.debug.config和一个web.release.config文件,该文件在部署项目时与基本web.config文件合并。如果可以的话,我将继续回答我自己的问题。我发现我让事情变得更难了。你所要做的就是:
<?xml version="1.0" encoding="utf-8"?>
请注意,config.Save方法的使用显然很重要,它可以立即加载刚才设置的设置。除此之外,我还需要决定路径名和节类型。我发现下面的链接是最有用的。如果有人有更优雅的方式,我很想听听
<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>
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();
}