0

I have something very strange. I'm using ConfigurationManager to open configuration file like:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = path;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

everything works fine, but when I look at config.AppSettings.Settings.AllKeys I can see key named Counter that does not exist in my configuration file, in addition to the other keys.

In the configuration file:

  <appSettings>
    <add key="test" value="23"/>
  </appSettings>

Any ideas please?

Thank you in advanced

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3132295
  • 288
  • 4
  • 23

2 Answers2

2

.NET configuration system uses an inheritance mechanism. Some settings are defined in a machine-level configuration file (<.NET Framework directory>\machine.config), and each app-specific configuration file can override machine-level settings. In your case, I think the Counter setting is defined in the machine.config file.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • also note that there are two versions of machine.config, for both x86 and x64 versions, if you are using x64 Windows. – Yura Apr 08 '15 at 12:02
1

Ar Thomas Levesque pointed out - you get inherited values from 'parent' configs on your machine. Sometimes you really don't want it happening. So you can use <clear\> element:

<appSettings>
     <clear/>
     <add key="test" value="23"/>
</appSettings>

This will prevent any automatic inheritance of <appSettings> collection.

Community
  • 1
  • 1
Yura
  • 2,013
  • 19
  • 25