5

I'm trying to setup 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 for prompts are comment or CDATA prompts

When I try to access in code

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

I get this error: {"An error occurred 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?

1 Answer 1

2

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

If not change the property for the config file Build Action - Content Copy to Output Directory - Copy Always

Edited:

This worked for me after adding public key token and change the name to 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>
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I can find the TestForm.exe.Config, but still getting same error
It is silly but I had done this mistake several times, if you are running in release mode check your bin\release if it is in debug mode check bin\debug
What is says is, it cannot load my System, Version=4.0.0.0, but that file is in C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0. However its version is 4.0.30319.1.
I'm in Debug, and the config is in Debug folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.