Determining whether running in debug/release mode

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

How do I determine whether Im running in debug or release mode from code
please.
thanks
Claire
 
By default, Visual Studio(if that is the case) defines the conditional
compilation constant
"DEBUG" when compiling in DEBUG mode. The constant is removed when switching
to RELEASE
mode.
Therefore, you can use precompilator directives to determine the current
configuration;
#if DEBUG
Console.Write("DEBUG mode");
#else
Console.Write("RELEASE mode");
#endif

I believe you may also use System.Diagnostics.Debugger.IsAttached
to determine the configuration.
However, the first alternative would be recommended.

Regards,
Dennis JD Myrén
Oslo Kodebureau
 
I think this should do the trick ...

bool runningInDebug = false;

#if DEBUG

runningInDebug = true;

#endif
 
Use conditional compilation blocks in code

#if DEBUG
.. statements here only included in debug version
#endif

If you on the other hand mean "running in debugger" and not wether it is a
module compiled in debug or release mode (release mode compiled modules may
also be debugged) you can check the System.Diagnostics.Debugger.IsAttached
static property.


Arild
 
Claire,

While most of these suggestions are acceptable, I think that you should
use reflection to determine whether or not the assembly has the Debuggable
attribute attached to it. Most compilers (should) attach this to any
assembly compiled in debug mode. The C# compiler definitely does, and I
think it would be safe to say that the other .NET compilers issued by MS do
as well.

In order to determine whether or not the assembly was compiled in debug
mode, get the Debuggable attribute from the assembly.

Assuming that you have an Assembly instance, this is how you would do
it:

// The assembly is in a variable named assembly.
object[] attributes =
assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);

// If the array is null, or has length of zero, then it is not debuggable.
if (!(attributes == null || attributes.length == 0))
{
// It is debuggable, figure out the level.
DebuggableAttribute debug = (DebuggableAttribute) attributes[0];

// At this point, you can access the DebuggingFlags property to
determine the level of debugging.
}

Hope this helps.
 
Back
Top