.Net配置-如何覆盖映射配置文件中的设置和自定义节
本文关键字:配置文件 设置 自定义 映射 配置 何覆盖 覆盖 Net | 更新日期: 2023-09-27 18:27:39
如果我的program.exe.config文件中有:
<configuration>
<configSections>
<section name="fooSection" type="MyNamespace.MySection, My.Assembly"/>
</configSections>
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
<fooSection>
<bar>
<add baz="foo bar baz"/>
</bar>
</fooSection>
</configuration>
我有overrides.config文件:
<configuration>
<configSections>
<section name="fooSection" type="MyNamespace.MySection, My.Assembly"/>
</configSections>
<appSettings>
<add key="foo" value="BAZ"/>
</appSettings>
<fooSection>
<bar>
<add baz2="foo foo"/>
</bar>
</fooSection>
</configuration>
是否有任何方法可以将其读取到核心(或加载)配置中,就像您在machine->app配置文件先验中获得的那样?(甚至machine.config->machine root web.config->local app web.config)
在这个例子中,我想要config。AppSettings["foo"]为"BAZ",fooSection包含两项:"foo-barbaz"answers"foo-foo"。
我找不到办法,我猜这可能得不到支持。我试过很多种排列方式。
MySection section = ConfigurationManager.GetSection("foo") as MySection;
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.LocalUserConfigFilename = // have tried this;
fileMap.ExeConfigFilename = // have tried this
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("foo");
foo = ConfigurationManager.GetSection("foo") as MySection;
// will only have one item
foo = config.GetSection("foo") as MySection;
// will only have one item
为什么要这样做?在我看来,这似乎是你试图处理生产配置?如果是这样的话,那么我建议查看.config转换。事实上,Scott Hanselman写了一篇关于的好文章
否则,我认为这是不可能的。尤其是配置转换的灵活性。