.NET - 在App.Config

时间:2018-04-20 13:48:03

标签: c# configuration app-config configurationsection

我有一个.NET 4.7应用程序。在这个应用程序中,我有几个我想在App.config文件中使用的自定义配置元素。出于这个原因,我在我的项目中定义了几个与配置相关的类。这个项目编译,结构如下:

./
  /ConfigurationItems
    Children
    Child
  App.config
  Program.cs

在我的App.config文件中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="Children" type="MyOrg.MyApp.ConfigurationItems.Children, MyOrg.MyApp.exe" />
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>  

  <Children>
    <Child Name="Joe" />
    <Child Name="Suzy" />
  </Children>  
</configuration>

当我运行此应用程序时,我会转到以下行:

  var children = ConfigurationManager.GetSection("Children") as MyOrg.MyApp.ConfigurationItems.Children;

此时,我收到以下异常:

System.Configuration.ConfigurationErrorsException: 'An error occurred creating the configuration section handler for Children: Could not load file or assembly 'MyOrg.MyApp.exe' or one of its dependencies. The system cannot find the file specified.'

FileNotFoundException: Could not load file or assembly 'MyOrg.MyApp.exe' or one of its dependencies. The system cannot find the file specified.

我做错了什么?如何使用项目中定义的自定义配置元素?

谢谢

1 个答案:

答案 0 :(得分:0)

type元素移除<section>属性中的“.exe”文件扩展名:

  <configSections>
    <section name="Children" type="MyOrg.MyApp.ConfigurationItems.Children, MyOrg.MyApp" />
  </configSections>
相关问题