首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在.net 4.0中设置configSection

如何在.net 4.0中设置configSection
EN

Stack Overflow用户
提问于 2012-03-27 22:31:03
回答 1查看 6.3K关注 0票数 5

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

代码语言:javascript
复制
<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提示

当我尝试在代码中访问时

代码语言:javascript
复制
object obj = ConfigurationManager.GetSection("MonitorFldrSection");

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

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

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-27 22:34:52

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

如果没有,请更改配置文件Build Action - Content Copy to Output Directory - Copy Always的属性

编辑:

在添加公钥标记并将名称更改为key后,这对我很有效

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9891635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档