single app.config multi-project c#

I want to use a single app.config by 3 different projects.

How to access the configurations?

ConfigurationManager.AppSettings["config1"]

Let's say you have this folder structure:

  • Solution
    • Project1
    • Project2
    • Project3

Do this:

  1. Create the App.config file in the Solution level folder. You won't find an option to add an App.config file from the templates, so just create a new empty text file with the name App.config, and paste in the contents of a regular App.config file.
  2. For each project in Solution Explorer:

    1. Right click and select Add > Existing Item
    2. Locate the file
    3. Select Add as link from the drop down box next to the Add button.

      Add as link

Edited to add:

You correctly state that the above method only shared the file up to build-time. To use a shared file at run-time, see the answers to this question.


The common config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section 
            name="appSettings" 
            type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
            />
    </configSections>
    <appSettings>
        <add key="key1" value="value1"/>
    </appSettings>
</configuration>

To access mapped config file

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string value = configuration.AppSettings.Settings["key1"].Value;

I have found the button, and opened the app.config as link, however that caused when build to again create separate config file for each project, and therefore, when deploying the 3 project, i will have 3 config files. What I wanted to do, is keeping a single file for all projects in a certain solution. Can I do that?

Yes - you can do it, but should you do it?

The basic assumption in a .NET app is that one app = one config file. Out of the box, and with an easy method, you cannot share config files between applications.

If you create your own custom config sections, you could "outsource" those to external files, which could be shared. Imagine you create your own custom config section called "MyConfiguration", then your app.config would look something like that:

<configuration>
  <configSections>
    <section name="MyConfiguration" 
             type="MyConfigurationSection, MyConfigurationAssembly" />
  </configSections>

  <MyConfiguration>
    <nestedElement>
      <dateTimeValue>10/16/2006</dateTimeValue>
      <integerValue>1</integerValue>
    </nestedElement>
  </MyConfiguration>
</configuration>

You could have your "MyConfiguration" section in its own file, and reference it from your app's config:

<configuration>
  <configSections>
    <section name="MyConfiguration" 
             type="MyConfigurationSection, MyConfigurationAssembly" />
  </configSections>

  <MyConfiguration configSource="MyConfiguration.config" />
</configuration>

and your "MyConfiguration.config" would then contain:

  <MyConfiguration> 
    <nestedElement>
      <dateTimeValue>10/16/2006</dateTimeValue>
      <integerValue>1</integerValue>
    </nestedElement>
  </MyConfiguration>

By doing this, you could "externalize" and thus share at least the bulk of your config settings - provided they're in your own custom config sections.

For more info and an excellent intro to .NET 2.0 and up configuration mysteries, see Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful!

Marc