How to set up configuration in .net 4.0

I am trying to set up configSection for a .net 4.0 project.

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

      

However, when I try to add a key, all I get is for hints, comment or CDATA hint

When I try to access in code

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

      

I get this error: {"An error occurred while creating the configuration section handler for MonitorFldrSection: Could not load file or assembly 'System, version = 4.0.0.0' or one of its dependencies. The system cannot find the file specified. (C: \ Projects_4 .0 \ NasImageIndexer \ TestForm \ bin \ Debug \ TestForm.exe.Config line 5) "}

Along with NameValueFileSectionHandler, I've also tried AppSettingsSection and DictionarySectionHandler.

What am I doing wrong?

+3


source to share


1 answer


Can you find this file in the C: \ Projects_4.0 \ NasImageIndexer \ TestForm \ bin \ Debug \ TestForm.exe.Config folder?

If you do not change the property for the Build Action configuration file - Contents Copy to output directory - Copy always

Edited:



This worked for me after adding the public key token and replaced the name on the key instead

<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


source







All Articles