llasarov
llasarov

Reputation: 2111

How to retrieve .NET executable app.config merged with machine.config

In my current project we have a .NET console application with some configurations (app.config, custom ConfigurationSection, etc.). In the configuration, several paths to files on the local file system are specified. Since the paths on the individual developer machines may differ, I'd like the specify them in machine.config and not in app.config, so every developer can "overwrite" them with his own paths.

So in the app.config I register the configSection (element 'configSections') but in the config section I don't define the paths. In the machine.config I register the configSection and add the configSection with my paths.

It looks like this:

app.config:

<configSections>
  <section name="importingExec" 
           type="ImportingExecutableConfigSection, Executable" />
</configSections>

<importingExec>
  <!-- xmlSchema xmlSchemaPath="D:\foo.xsd"/ -->
</importingExec>

machine.config:

<configSections>
  <section name="importingExec" 
           type="ImportingExecutableConfigSection, Executable" />
</configSections>

<importingExec>
  <xmlSchema xmlSchemaPath="D:\foo.xsd"/>
</importingExec>

I have following problem: when I retrieve the configuration it throws an exception since the config section (required!) is missing. I've expected that the values from machine.config will be returned!

P.S.: I retrieve the config section by invoking

ConfigurationManager
    .OpenExeConfiguration(ConfigurationUserLevel.None)
    .GetSection("importingExec");

Upvotes: 3

Views: 292

Answers (1)

Chris Camplejohn
Chris Camplejohn

Reputation: 26

You are explicitly requesting the exe's config file by using that code.

You should use

ConfigurationManager.GetSection("importingExec")

in order to get the merged files.

Cheers Chris

Upvotes: 1

Related Questions