Programmatically check the build configuration

Paul Michaels picture Paul Michaels · Jul 15, 2016 · Viewed 8.9k times · Source

Using the DEBUG configuration, I can switch behaviour on and off using this type of syntax:

#if DEBUG
    Console.WriteLine("Debug");
#else
    Console.WriteLine("Not Debug");
#endif

However, if I set up a different configuration, say: TEST then this doesn't work:

#if TEST
    Console.WriteLine("Test");
#else
    Console.WriteLine("Not Test");
#endif

Is there a way to check these?

Answer

eldrex picture eldrex · Jul 15, 2016

Yes you can use different configurations. DEBUG symbol is generated automatically if you choose Debug configuration in your configuration manager. You can check it. Go to Your project -> Properties -> Build -> Define DEBUG constant

If you need to use additional constant just enter your own in Conditional compilation symbols.

Steps for your case:

  1. Go to Your project -> Properties -> Build
  2. Switch configuration to Test
  3. Enter TEST to Conditional compilation symbols field

Run your code and enjoy :)