Korey
Korey

Reputation: 999

Visual Studio: differentiate app.config for debug and release mode

Is there a way to automatically use a separate app.config when building in release mode?

In other words, I want to test with one app.config, and release with another.

Currently, I keep a separate copy called app.config.production, and manually overwrite bin\Release\Application.exe.config after building for release.

Upvotes: 68

Views: 71390

Answers (6)

Matthieu H
Matthieu H

Reputation: 717

A clean solution is to group 2 files App.Debug.config and App.Release.config into App.config and change the good file into App.config depending on the configuration at compile time:

<ItemGroup>
    <None Include="App.config" />
    <None Include="App.Debug.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
    <None Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
</ItemGroup>
<Target Name="SetAppConfig" BeforeTargets="Compile">
    <Copy SourceFiles="App.Debug.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Debug' " />
    <Copy SourceFiles="App.Release.config" DestinationFiles="App.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)' == 'Release' " />
</Target>

With this solution you will get something like this in Visual Studio:

screenshot

Upvotes: 22

Mark Z.
Mark Z.

Reputation: 2447

Similar to top answer but with this approach you can see the actual file if preferred and intellisense doesn't complain in csproj file:

  <Target Name="SetAppConfig" BeforeTargets="Compile">
    <Copy SourceFiles="debug.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
    <Copy SourceFiles="release.config" DestinationFiles="app.config" OverwriteReadOnlyFiles="true" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
  </Target>

Upvotes: 5

trueboroda
trueboroda

Reputation: 2880

Unload the project in Solution Explorer via the context menu.

Edit the .csproj file via the context menu and add this:

<PropertyGroup>
    <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>

Upvotes: 59

Rudy Hinojosa
Rudy Hinojosa

Reputation: 1468

I highly recommend SlowCheetah for app.config transformations. Visit this nuget gem here Visual Studio Gallery

Upvotes: 10

Dave
Dave

Reputation: 3717

I don't know if this helps, but app.config will recognise the standard MSBUILD substitution strings such as $(Configuration).

Upvotes: 1

Martin Braun
Martin Braun

Reputation: 12589

A simple and fast way is to create a second file "App.release.config" and insert this pre-build event:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.config" "$(ProjectDir)App.debug.config"
IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.release.config" "$(ProjectDir)App.config"

And this post build event:

IF $(ConfigurationName) == Release COPY /Y "$(ProjectDir)App.debug.config" "$(ProjectDir)App.config"

This might be a bit odd, but it will allow you to keep using the .Settings files as debug settings, that are still linked to the App.config. The App.release.config must be build by hand, but it's pretty easy to switch this functionality.

Upvotes: 10

Related Questions