Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a configSection in .net 4.0

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?

like image 269
edepperson Avatar asked Nov 04 '22 03:11

edepperson


1 Answers

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>
like image 132
Kiru Avatar answered Nov 09 '22 11:11

Kiru