2012-07-09 60 views
3

在我當前的項目中,我們有一個帶有一些配置(app.config,自定義的ConfigurationSection等)的.NET控制檯應用程序。在配置中,指定了本地文件系統上的多個文件路徑。由於各個開發者機器上的路徑可能不同,我想在machine.config中指定它們,而不是在app.config中指定它們,因此每個開發人員都可以用他自己的路徑「覆蓋」它們。如何檢索與machine.config合併的.NET可執行文件app.config

所以在app.config我註冊configSection(元素'configSections'),但在配置部分我沒有定義路徑。在machine.config中,我註冊了configSection並添加了configSection和我的路徑。

它看起來像這樣:

的app.config:

<configSections> 
    <section name="importingExec" 
      type="ImportingExecutableConfigSection, Executable" /> 
</configSections> 

<importingExec> 
    <!-- xmlSchema xmlSchemaPath="D:\foo.xsd"/ --> 
</importingExec> 

的machine.config:

<configSections> 
    <section name="importingExec" 
      type="ImportingExecutableConfigSection, Executable" /> 
</configSections> 

<importingExec> 
    <xmlSchema xmlSchemaPath="D:\foo.xsd"/> 
</importingExec> 

我有以下問題:當我檢索配置它拋出一個異常,因爲配置部分(必需!)丟失。我期望machine.config的值將被返回!

P.S:我通過調用

ConfigurationManager 
    .OpenExeConfiguration(ConfigurationUserLevel.None) 
    .GetSection("importingExec"); 

回答

1

您是通過使用代碼明確要求的exe的配置文件檢索配置部分。

您應該使用

ConfigurationManager.GetSection("importingExec") 

,以獲得合併後的文件。

Cheers Chris