Does anyone know how to get the current build configuration $(Configuration)
in C# code?

- 31,141
- 6
- 88
- 120

- 9,602
- 25
- 89
- 136
7 Answers
There is AssemblyConfigurationAttribute in .NET. You can use it in order to get name of build configuration
var assemblyConfigurationAttribute = typeof(CLASS_NAME).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
var buildConfigurationName = assemblyConfigurationAttribute?.Configuration;

- 416
- 8
- 24
-
I've edited my accepted answer and linked to this. Good find Egor :) – Binary Worrier Feb 25 '22 at 09:16
If you unload your project (in the right click menu) and add this just before the </Project>
tag it will save out a file that has your configuration in it. You could then read that back in for use in your code.
<Target Name="BeforeBuild">
<WriteLinesToFile File="$(OutputPath)\env.config"
Lines="$(Configuration)" Overwrite="true">
</WriteLinesToFile>
</Target>

- 78,325
- 149
- 468
- 850
-
I have used Vaccano's solution. I have to dynamically change some configuration basing on each build configuration, not just debug or release, and this solution works out great for me. Thanks. – MsBugKiller Nov 25 '13 at 17:40
-
I used this in common with a Wix# (WixSharp) installer program to have my C# console app "Release" build binary & config files fed to the Wix# installer, and it worked great for my needs! (Wix# is based on the Windows Installer XML tools for building MSI installers, https://wixtoolset.org/, and Wix# allows you to write your installer in C# code that generates the WiX xml files, instead of "coding in XML". https://github.com/oleg-shilo/wixsharp/wiki/Building-MSI-%E2%80%93-Step-by-step-tutorial. Also see https://www.infoq.com/articles/WixSharp/. – Developer63 Nov 07 '19 at 20:34
-
Thanks @vaccano - I'm using this to handle loading the correct appsettings.environment.json for integration tests.
Update
Egors answer to this question ( here in this answer list) is the correct answer.
You can't, not really.
What you can do is define some "Conditional Compilation Symbols", if you look at the "Build" page of you project settings, you can set these there, so you can write #if statements to test them.
A DEBUG symbol is automatically injected (by default, this can be switched off) for debug builds.
So you can write code like this
#if DEBUG
RunMyDEBUGRoutine();
#else
RunMyRELEASERoutine();
#endif
However, don't do this unless you've good reason. An application that works with different behavior between debug and release builds is no good to anyone.

- 50,774
- 20
- 136
- 184
-
1It may help when developing Windows Services (no need to install / easier debugging) – Scoregraphic May 06 '09 at 12:38
-
@Scoregraphic: However with quite different behavior (service vs. command line app)!!! – Dirk Vollmar May 06 '09 at 12:49
-
Why is conditional expression not finding my custom configuration ? I have code like this: #if WCFDebug /*my code*/ #endif - but it simply doesn't find it. I've added configuration inside settings. – FrenkyB Apr 11 '15 at 09:15
-
-
1@Marcus Then define conditional compilation symbols for those as well in your project properties. – Elkvis Apr 30 '20 at 21:34
-
@Marcus the above answer might have been true several years ago, but modern versions of .net let you get the info. See Egor Novikov's answer below. – David Feb 22 '22 at 18:50
Conditional Compilation Symbols can by used to achieve this. You can define custom symbols the Properties > Build settings pane for each project, and the use the #if directives to test them in the code.
Example showing how the define the symbol UNOEURO and how to use it in code.
bool isUnoeuro = false;
#if UNOEURO
isUnoeuro = true;
#endif

- 10,940
- 8
- 45
- 80
- Install the SlowCheetah Visual Studio extension.
- Right-click on your config file and select 'Add Transform'.
- Notice a transform for each build configuration.
- Place a "Build" appSetting into the root config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<add key="Build" value="" />
</appSettings>
</configuration>
- And place a "Build" directive in each transform:
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Build" value="Debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
- Then obtain the "Build" appSetting value in your C# code:
ConfigurationManager.AppSettings["Build"]
- Please be mindful that you will not see the transforms when you are debugging: https://stackoverflow.com/a/17336009/109941

- 15,141
- 22
- 103
- 166
You can use a common static method with Conditional Attribute to set the flag to detect DEBUG or RELEASE mode. The SetDebugMode method will be called only when running in the DEBUG mode otherwise it is ignored by Runtime.
public static class AppCompilationConfiguration
{
private static bool debugMode;
private static bool IsDebugMode()
{
SetDebugMode();
return debugMode;
}
//This method will be loaded only in the case of DEBUG mode.
//In RELEASE mode, all the calls to this method will be ignored by runtime.
[Conditional("DEBUG")]
private static void SetDebugMode()
{
debugMode = true;
}
public static string CompilationMode => IsDebugMode() ? "DEBUG" : "RELEASE";
}
You can call it in the code like below
Console.WriteLine(AppCompilationConfiguration.CompilationMode);

- 1,881
- 2
- 18
- 26
I don't believe you can inject that at compile time into the assembly but one way you could achieve it would be to use MSBuild and add it to the config file of the application.
See this blog post about how to do multi-environment config files using MSBuild - http://adeneys.wordpress.com/2009/04/17/multi-environment-config/
Alternatively you could write an MSBuild task which would edit a certain compiled file (your C# or VB file) and have that run in the BeforeBuild
task. It'd be rather tricky as you'd need to work out where to inject it into the file, but provided you had some kind of tokenization set up you should be able to do it. I also doubt it would be pretty!

- 24,927
- 18
- 98
- 150