29

I am creating one class library project. Now by default I have one App.Config file so that I am putting all environment specific data in that Config file.

Now based on the Environment (whether Dev / Test / Production), I am planning to have three App.Config files in VS 2010 such as

App.Dev.Config

App.Test.Config

App.Prod.Config

Wondering how would the application know which config file to use.

Anyone implemented this scenario. Any Code samples / Articles would be helpful.

Thanks

one noa
  • 345
  • 1
  • 3
  • 10
Rita
  • 911
  • 4
  • 16
  • 31
  • For VB .NET solution refer to [this](https://stackoverflow.com/q/55547789/465053) post – RBT Jan 27 '21 at 10:01

3 Answers3

18

The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project. Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.

  1. Option: You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder

  2. Option: You can use the ConfigurationManager Class to load an alternate config file by code.

Jan
  • 15,802
  • 5
  • 35
  • 59
  • 7
    +1: and just to be very clear here: The config files associated with an assembly project will NEVER be used. Only the main project config files will be used. You need to control them at that level. – NotMe Dec 15 '10 at 20:39
  • @NotMe: I know it's a bit more explicit from what you had in mind, but there are ways to include library config files. Give them a unique name, set them to copy to output directory, and have your startup project's app.config reference them as an external link. – Flater Dec 04 '17 at 16:59
12

Loading a different application configuration file at run time can be done using the concept of mapped configuration file. To start with, you need to add reference to System.Configuration.dll in your project.

Set the value of Copy to Output Directory property to Copy if newer (Refer screenshot). This has to be done only for non-default configuration files e.g. App1.config, App2.config, etc. Leave the default configuration file namely App.config as it is . Due to this change, all the non-default application configuration files will be available in the project output directory (\bin\debug) when the project is built. Default value of this property is Do not copy.

enter image description here

Here is the code snippet on how to read configuration data from non-default configuration files:

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App1.config"; // app1.config should be present in root directory from where application exe is kicked off

 // Get the mapped configuration file
 var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

 //get the relevant section from the config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

//get key value pair
var keyValueConfigElement = section.Settings["appSettingsKey"];
var appSettingsValue = keyValueConfigElement.Value;

If you have multiple application (aka app) configuration files then you can keep a setting in default App.config file with the help of which you can make a decision at run time about which non-default configuration file to load e.g. App1.config

Note: Now look at below code:

ConfigurationManager.AppSettings["DeployEnv"]

This code will still read the data from the default App.config file. This behavior can't be changed. There is no way to prohibit the Loading of default App.config file. You have to use alternate means as discussed in this post to read the data from non-default configuration files

RBT
  • 24,161
  • 21
  • 159
  • 240
  • 2
    Thank you for solution! I made conversion for VB: https://stackoverflow.com/questions/55547789/vb-get-configuration-from-different-configuration-file – Grufas Apr 06 '19 at 19:24
  • Really appreciate this while converting app from .NET Framework to .NET6 while `sample.exe.config` changed to `sample.dll.config` but my whole eco-system expects to work with exe config file directly. – Jan Zahradník Aug 12 '22 at 06:29
11

Now there is an even better solution: SlowCheetah - XML Transforms

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
duedl0r
  • 9,289
  • 3
  • 30
  • 45