【问题标题】:How to setup a configSection in .net 4.0如何在 .net 4.0 中设置 configSection
【发布时间】:2012-04-11 02:35:46
【问题描述】:

我正在尝试为 .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>

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

当我尝试在代码中访问时

object obj = ConfigurationManager.GetSection("MonitorFldrSection");

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

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

我做错了什么?

【问题讨论】:

    标签: c#-4.0 app-config


    【解决方案1】:

    你能在 C:\Projects_4.0\NasImageIndexer\TestForm\bin\Debug\TestForm.exe.Config 位置找到这个文件吗?

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

    已编辑:

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

    <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>
    

    【讨论】:

    • 是的,我可以找到 TestForm.exe.Config,但仍然出现同样的错误
    • 这很愚蠢,但我已经多次犯过这个错误,如果您在发布模式下运行,请检查您的 bin\release 如果它处于调试模式,请检查 bin\debug
    • 也就是说,它无法加载我的系统,版本=4.0.0.0,但该文件位于 C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 中。但是它的版本是 4.0.30319.1。
    • 我在Debug,配置在Debug文件夹中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2018-10-18
    • 2022-01-12
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多