user3132295
user3132295

Reputation: 297

When reading appSettings, I get key Counter from nowhere

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

Upvotes: 0

Views: 59

Answers (2)

Yura
Yura

Reputation: 2431

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.

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292695

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

Upvotes: 2

Related Questions