2012-03-27 50 views
3

我正在尝试为.net 4.0项目设置configSection。如何在.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创建配置节处理程序时发生错误:无法加载文件或程序集'System,Version = 4.0.0.0'或其某个依赖项。系统找不到指定的文件(C:\ Projects_4.0 \ NasImageIndexer \ TESTFORM \ BIN \调试\ TestFor m.exe.Config line 5)“}

随着NameValueFileSectionHandler,我也试过AppSettingsSection和DictionarySectionHandler。

我在做什么错?

回答

0

您能在位置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> 
+0

是的,我可以找到TestForm.exe.Config,但仍然getti ng相同的错误 – edepperson 2012-03-27 14:40:07

+0

这是愚蠢的,但我已经多次犯了这个错误,如果你在release模式下运行,检查你的bin \ release,如果它处于调试模式check bin \ debug – Kiru 2012-03-27 14:45:02

+0

说的是,它无法加载我的系统,版本= 4.0.0.0,但该文件位于C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.0中。但其版本是4.0.30319.1。 – edepperson 2012-03-27 14:45:29