首页 文章

如何检索与machine.config合并的.NET可执行app.config

提问于
浏览
3

在我当前的项目中,我们有一个带有一些配置的.NET控制台应用程序(app.config,自定义ConfigurationSection等) . 在配置中,指定了本地文件系统上文件的多个路径 . 由于各个开发人员计算机上的路径可能不同,我希望在machine.config中指定它们而不是在app.config中,因此每个开发人员都可以使用自己的路径“覆盖”它们 .

所以在app.config中我注册了configSection(元素'configSections')但是在config部分我没有定义路径 . 在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 回答

  • 1

    您通过使用该代码显式请求exe的配置文件 .

    你应该用

    ConfigurationManager.GetSection("importingExec")
    

    为了获得合并的文件 .

    干杯克里斯

相关问题