Multiple App.Config Files in .NET Class library project

Rita picture Rita · Dec 15, 2010 · Viewed 41.8k times · Source

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

Answer

Jan picture Jan · Dec 15, 2010

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.