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

7

68

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.

Assimilative answered 18/8, 2009 at 17:54 Comment(0)
R
57

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>
Rounding answered 16/5, 2018 at 14:45 Comment(2)
although intellisense does not show appconfig as a valid property. It's works. I think it is most useful in the final stage of development, when no more major changes are needed in the app.config file. thanksFerullo
This even works with .NET 7. Thanks a lot!Caves
S
21

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

Scleritis answered 8/7, 2021 at 16:8 Comment(2)
But this solution just overrides App.config with either debug or release version, if you shared settings for both you will still have to maintain them in both configs so that it does not gets removed.Fisk
Depending on your build target it will copy App.config to YourProject.exe.config before this target runs. So in this case it's better to use BeforeTargets="Compile;_CopyAppConfigFile"Rickierickman
A
20

I have recently posted a supremely belated response to a similar SO topic: https://mcmap.net/q/282230/-different-application-settings-depending-on-configuration-mode

I will repeat it here for clarity:

This is somewhat late to the party, but I stumbled upon a nice way of implementing the web.transform approach for app.config files. (i.e. it makes use of the namespace http://schemas.microsoft.com/XML-Document-Transform)

I think it is "nice" because it is a pure xml approach and doesn't require 3rd party software.

A parent / default App.config file is descended from, according to your various build configurations. These descendants then only override what they need to. In my opinion this is much more sophisticated and robust than having to maintain x number of config files which get copied in their entirety, such as in other answers.

A walkthrough has been posted here: http://mitasoft.wordpress.com/2011/09/28/multipleappconfig/


Look, Mom - No explicit post-build events in my IDE!

Alopecia answered 18/12, 2014 at 12:35 Comment(3)
For VS 2017, v10.0 has to be replaced with v15.0 in <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Publishing.targets" /> in the walk-through above.Philpot
Im using Visual Studio 2022 with a .net 6 web api, this solution did not work.Fidelia
v10.0 should be replaced with v$(VisualStudioVersion) to make this solution compatible with any VS versionDestruction
K
10

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.

Korman answered 26/11, 2015 at 10:1 Comment(3)
where do you add these build events?Quenchless
@Quenchless Right click your project in your solution, click on "Properties" and open the tap "Build Events"Korman
I really like this, simple and effective and doesnt rely on msbuild magic which tends to change over time.Clockmaker
G
10

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

Ghiselin answered 18/8, 2016 at 15:50 Comment(5)
Side Note: SlowCheetah only works when you publish your works, not when debugging.Ghiselin
that is no longer the caseLove
Nice! I was hoping that change would come down the pike soon.Ghiselin
SlowCheetah is a much better solution than Pre build / Post build scripting.Mountford
VS 2022 Support too! github.com/Microsoft/slow-cheetah marketplace.visualstudio.com/…Gillan
L
5

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>
Ludmilla answered 27/4, 2020 at 5:57 Comment(0)
D
1

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

Dominic answered 15/1, 2016 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.