2

If I have in myprogram.exe.config file:

<configuration>
  <configSections>
    <section name="fooSection" type="MyNamespace.MySection, My.Assembly"/>
  </configSections>
  <appSettings>
    <add key="foo" value="bar"/>
  </appSettings>
  <fooSection>
    <bar>
      <add baz="foo bar baz"/>
    </bar>
  </fooSection>
</configuration>

and I have in overrides.config file:

<configuration>
  <configSections>
    <section name="fooSection" type="MyNamespace.MySection, My.Assembly"/>
  </configSections>
  <appSettings>
    <add key="foo" value="BAZ"/>
  </appSettings>
  <fooSection>
    <bar>
      <add baz2="foo foo"/>
    </bar>
  </fooSection>
</configuration>

Is there any way to have this read into the core (or loaded) configuration, like you would get with the machine->app config file precendence? (or even machine.config -> machine root web.config -> local app web.config)

In this example, I would like config.AppSettings["foo"] to be "BAZ" and fooSection to contain two items: "foo bar baz" and "foo foo".

I cannot find a way, and am guessing it may not be supported. I've tried many permutations.

MySection section = ConfigurationManager.GetSection("foo") as MySection;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.LocalUserConfigFilename = // have tried this;
fileMap.ExeConfigFilename = // have tried this
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("foo");

foo = ConfigurationManager.GetSection("foo") as MySection;
// will only have one item
foo = config.GetSection("foo") as MySection;
// will only have one item
Troy Parsons
  • 531
  • 1
  • 6
  • 11
  • Were you able to solve this problem ever? I have similar requirement where i have to override the default config with client specific configs. – Sri Reddy Aug 21 '18 at 13:47

1 Answers1

0

Why are you looking to do this? This seems to me like you are trying to maybe deal with production configs? If that is the case, then I would suggest looking at .config transforms. In fact, Scott Hanselman wrote up a good post on the subject

Otherwise, I do not believe that this is possible. Especially, with the flexibility in config transforms.

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • Thanks, Justin. This may be useful in some situations. Right now I run regression tests of a product, and can specify -config path\to.config to test certain functionality, so a change of the build philosophy could probably make this fit. – Troy Parsons Mar 14 '12 at 07:05
  • The other scenario, is that the program may not be run from a local path, but from a network share (easier distribution of software). Say for instance it is run as a console app, or a Windows service - you could specify command line options, or direct to a configuration file that is tailored for the local host. – Troy Parsons Mar 14 '12 at 07:07
  • May it will help, – Arun Singh Mar 27 '12 at 17:54