Multiple App.Config Files in .NET Class library project
Asked Answered
W

4

30

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

Whitesmith answered 15/12, 2010 at 19:31 Comment(1)
For VB .NET solution refer to this postGrefer
S
19

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.

Sophomore answered 15/12, 2010 at 19:34 Comment(2)
+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.Eudoxia
@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.Philosopher
G
13

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

Grefer answered 30/8, 2016 at 6:29 Comment(2)
Thank you for solution! I made conversion for VB: #55548289Palmer
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.Hakodate
L
11

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

Lubalubba answered 17/12, 2012 at 16:45 Comment(0)
A
1

Simplest way to do this without using any 3rd party extension or without writing a single line of code goes like this...

Assuming the config files for each environment are named like these in the root of your project structure: app.debug.config app.release.config etc... (You don't need to have a default app.config anymore)

You may create more build configurations and app config files for each of them based on your environments.

Add this line to the projects post-build event:

copy "$(ProjectDir)app.$(Configuration).config" "$(TargetPath).config"
Antihalation answered 3/4, 2024 at 23:25 Comment(1)
Simple and working.Handbook

© 2022 - 2025 — McMap. All rights reserved.