【发布时间】:2012-03-30 13:49:50
【问题描述】:
如果我在 myprogram.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.config -> 机器根 web.config -> 本地应用程序 web.config)
在本例中,我希望 config.AppSettings["foo"] 为“BAZ”,而 fooSection 包含两个项目:“foo bar baz”和“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
【问题讨论】:
-
你曾经解决过这个问题吗?我有类似的要求,我必须用客户端特定的配置覆盖默认配置。
标签: c# .net configuration app-config