1
如果我有myprogram.exe.config文件:.Net配置 - 如何覆蓋映射配置文件中的設置和自定義部分?
<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
謝謝,賈斯汀。這在某些情況下可能會有用。現在我運行一個產品的迴歸測試,並且可以指定-config path \ to.config來測試某些功能,所以對構建理念的改變可能會使這一點適合。 – 2012-03-14 07:05:35
另一種情況是,程序可能不是從本地路徑運行,而是從網絡共享運行(軟件分發比較容易)。假設它是作爲控制檯應用程序或Windows服務運行的 - 您可以指定命令行選項,或指向爲本地主機量身定製的配置文件。 – 2012-03-14 07:07:39
它可能會幫助, –
2012-03-27 17:54:04