如何在.net 4.0中设置configSection

ede*_*son 5 app-config c#-4.0

我正在尝试为.net 4.0项目设置configSection。

<configuration>
  <configSections>
    <section name="MonitorFldrSection"
         type="System.Configuration.NameValueFileSectionHandler, System, Version=4.0.0.0"
          allowLocation="true"
          allowDefinition="Everywhere"/>
  </configSections>
  <MonitorFldrSection>
    <add name="fldr1" value="C:\Temp" />
    <add name="fldr2" value="C:\Projects" />
  </MonitorFldrSection>
  <connectionStrings>
  </connectionStrings>
  <appSettings>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试添加密钥时,得到的提示都是注释或CDATA提示

当我尝试访问代码

object obj = ConfigurationManager.GetSection("MonitorFldrSection");
Run Code Online (Sandbox Code Playgroud)

我收到此错误:{“为MonitorFldrSection创建配置节处理程序时发生错误:无法加载文件或程序集'System,Version = 4.0.0.0'或其依赖项之一。系统找不到指定的文件。(C: \ Projects_4.0 \ NasImageIndexer \ TestForm \ bin \ Debug \ TestForm.exe.Config第5行)“}

除了NameValueFileSectionHandler之外,我还尝试了AppSettingsSection和DictionarySectionHandler。

我究竟做错了什么?

Kir*_*iru 2

您可以在位置 C:\Projects_4.0\NasImageIndexer\TestForm\bin\Debug\TestForm.exe.Config 中找到此文件吗?

如果不更改配置文件的属性构建操作 - 内容复制到输出目录 - 始终复制

编辑:

添加公钥令牌并将名称更改为 key 后,这对我有用

<configuration>
  <configSections>
   <section name="MonitorFldrSection"
type="System.Configuration.NameValueFileSectionHandler, System,   Version=4.0.0.0,         Culture=neutral, PublicKeyToken=b77a5c561934e089"
      allowLocation="true"
      allowDefinition="Everywhere"/>
</configSections>
<MonitorFldrSection>
<add key="fldr1" value="C:\Temp" />
<add key="fldr2" value="C:\Projects" />
</MonitorFldrSection>
<connectionStrings>
</connectionStrings>
<appSettings>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)