How to obtain build configuration at runtime?

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;

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.


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>