Easily switching between configuration files with MSBuild

A couple of days ago i wrote about Easily switching between App.Config files with MSBuild. Christophe Gijbels, a fellow compuwarrior, pointed out that developers usually need to copy more than a single App.Config file… I would propose to add a Folder for each Customer that contains all the specific configuration files. Eg:

screenshot of solution explorer with proposed folder structures

Now i have to configure MSBuild so that the right files are copied into the OutDir:

<!-- Define the CustomerPath depending on the choosen Configuration -->
<PropertyGroup  Condition=" $(Configuration) == 'Customer1 Debug' ">
 <CustomerPath>Customer1</CustomerPath>
</PropertyGroup>
<PropertyGroup  Condition=" $(Configuration) == 'Customer2 Debug' ">
 <CustomerPath>Customer2</CustomerPath>
</PropertyGroup>

<!-- Define AppConfig in the CustomerPath  -->
<PropertyGroup>
 <AppConfig>$(CustomerPath)\App.config</AppConfig>
</PropertyGroup>

<!-- Find all files in CustomerPath, excluding AppConfig -->
<ItemGroup>
<CustomerFiles Include="$(CustomerPath)\*.*" Exclude="$(CustomerPath)\App.config" />
</ItemGroup>

<Target Name="AfterBuild">
<Copy SourceFiles="@(CustomerFiles)" DestinationFolder="$(OutDir)" SkipUnchangedFiles="true"/>
</Target>
This entry was posted in Visual Studio. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>