'How to obtain build configuration at runtime?

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



Solution 1:[1]

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>

Solution 2:[2]

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;

Solution 3:[3]

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.

UNOEURO symbol defined here

bool isUnoeuro = false;
#if UNOEURO
    isUnoeuro = true;
#endif

Solution 4:[4]

  1. Install the SlowCheetah Visual Studio extension.
  2. Right-click on your config file and select 'Add Transform'.

Add Transform

  1. Notice a transform for each build configuration.

Transform for each build configuration

  1. 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>
  1. 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>
  1. Then obtain the "Build" appSetting value in your C# code:

ConfigurationManager.AppSettings["Build"]


Solution 5:[5]

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);

Solution 6:[6]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaccano
Solution 2 Egor Novikov
Solution 3 Jan Aagaard
Solution 4
Solution 5 Jeeva Subburaj
Solution 6 Aaron Powell