Friday, 3 August 2007

MSBuild: How to work with multiple app.config files

The need to maintain various different app.config files is fairly common in any serious .NET development project. For example, you may want different configurations to apply in different environments, or you may want to maintain different app.config files for different customer configurations of your application.

Visual Studio 2005 does not seem to be very helpful in this respect, though the MSBuild process does offer some solutions. The most flexible way I've found to manage this is by conditionally changing the value of the $(AppConfig) property in the BeforeBuild target (in the projects .csproj/.vbproj file), depending on the value of the $(Configuration) parameter. For example:

<Target Name="BeforeBuild">
  <CreateProperty Value="My.app.config"
        Condition=" '$(Configuration)' == 'MyConfig' ">
    <Output TaskParameter="Value" PropertyName="AppConfig" />
  </CreateProperty>
</Target>

0 comments: