Imagine the following situation: One codebase, two customers with different Application Configuration files. How can we easily switch between the different configurations? By taking advantage of the Build Configurations functionality in Visual Studio we can easily switch between different configurations:
A brute-force solution would be to add a Post-build Event that copies the desired App.Config file to the destination directory. In the Microsoft.Common.targets file (usually at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) around line 725 you can read how MSBuild chooses the App.Config that is copied to the destination folder:
Choose exactly one app.config to be the main app.config that is copied to the destination folder.
The search order is:
- Choose the value $(AppConfig) set in the main project.
- Choose @(None) App.Config in the same folder as the project.
- Choose @(Content) App.Config in the same folder as the project.
- Choose @(None) App.Config in any subfolder in the project.
- Choose @(Content) App.Config in any subfolder in the project.
Thus, simply setting $(AppConfig) should be enough to make sure that MSBuild chooses the appropriate App.Config file. Here is an example of a csproj section that defines $(AppConfig) as App.Customer1.Config or App.Customer2.Config depending on the choosen Build configuration: