4

How can I detect Debug or Release mode from command line pre-build or post-build window?

I tested the code below, it works in code text window. Can it be convert to command line code? If can, how to do it, thanks.

bool debugging = false;
#if DEBUG
    debugging = true;
    // do something like to move ../debug/bin/ to somewhere.
#else
    debugging = false;
    // do something like to move ../debug/bin/ to somewhere.
#endif

Console.WriteLine(debugging);
Nano HE
  • 1,879
  • 7
  • 28
  • 39

2 Answers2

8

You can check the value of the $(ConfigurationName) variable.

It is different to what you used in your code sample. #if DEBUG is a conditional compilation directive which depends upon whether DEBUG has been defined as a symbol or not. The ConfigurationName variable depends upon what build configuration you have specified (which is independent of the conditional compilation symbols).

slugster
  • 49,403
  • 14
  • 95
  • 145
0
echo **** Visual Studio PREBUILD... **** 
echo **** Remember these are running from command line, aka BATCH commands 
echo **** watch out (include) single space after the conditional (and other ridiculous traps and pitfalls)
echo **** so I HIGHLY recommend getting out of BATCH and into Powershell as soon as possible.  Make prebuild a one-liner and then put additional code in your powershell... (you will thank me later)
echo **** Read more about why I'd rather claw my eyes out than program in BATCH here: https://www.tutorialspoint.com/batch_script/batch_script_if_else_statement.htm

if "Debug" == $(ConfigurationName) (
  echo OPTIONALLY just pass in the Configuration name and do all the logic and checking INSIDE the powershell.  Seriously, it is way better.
  powershell .\DevBox.Prebuild.ps1 -Config:$(ConfigurationName)
)

echo *** Much better to just check for Debug INSIDE the powershell
powershell .\DevBox.Prebuild.ps1 -Config:$(ConfigurationName)

Edit:

also see this answer for including powershell named parameters and solving the problem with spaces and escaping in (ProjectDir). How to escape spaces the parameters to a PS script running in VS post-build event

m1m1k
  • 1,375
  • 13
  • 14