Is my C# application running in debug mode? or: About pre-processor directives

All I wanted to do was check if my application was running in debug-mode or in release-mode. Searching MSDN library and Google (groups) both did not deliver the solution to me. I was about to let go when, out of the blue, a webpage showed up in my browser telling me the following:

Visual Studio automatically generates a pre-processor directive called DEBUG which is linked to the mode you run your application in. Beware: these directives are case sensitive...

This meant an easy solution for my 'problem'!

Pre-processor directives can be defined using #define (making the directive true) and #undef (making it false) in C#. When you check one of these directives using an #if ... #else ... #endif statement Visual Studio even greys out the code that will not be compiled, because of the directives. This gives you a pretty good idea which code will and which code will not be compiled into the final assembly.
Because of the pre-processor directive behaviour, you can easily create cross-platform applications by simply changing one directive. Changing that one directive may change complete pieces of code in your application.

The answer to my question was simple:

 #if (DEBUG)

  imgPath = GetDirectoryName(Application.ExecutablePath);

#else

  imgPath = GetDirectoryName(Application.ExecutablePath) + "\\images\\";

#endif

Published Thu, Mar 10 2005 5:36 PM by Rick van den Bosch
Filed under:

Comments

# re: Is my C# application running in debug mode? or: About pre-processor directives

Now that would be nice when developing internet applications :)

Monday, March 14, 2005 10:01 AM by Rick van den Bosch

# re: Is my C# application running in debug mode? or: About pre-processor directives

It can be, yes. But think about the benefits it can give you when you're developing one application for different platforms. This way, you can completely leave out platform dependent code by changing one directive. Sweet!

Monday, March 14, 2005 2:07 PM by Rick van den Bosch

# re: Is my C# application running in debug mode? or: About pre-processor directives

I use it for compiling with cach options or not for example. It is a great way to develop all kind of options in the application and only by recompile it make the application cach stuff or not.

You even can get the CPU type that is used while compiling, so you can specifie code for that CPU. Just loving it.

Wednesday, March 16, 2005 5:05 PM by Rick van den Bosch

# BloggingAbout.net

BloggingAbout.net

Friday, March 18, 2005 8:53 PM by TrackBack

# re: Is my C# application running in debug mode? or: About pre-processor directives

Very handy to know, thanks

Tuesday, February 13, 2007 2:17 AM by Tristan Smith

# re: Is my C# application running in debug mode? or: About pre-processor directives

Thanks Machan.It was help me to solve my problem.....

Thursday, August 30, 2007 6:58 AM by Chamin Galappaththi

# re: Is my C# application running in debug mode? or: About pre-processor directives

Problem is does visual studio automatically undefine #DEBUG

e.g  #undef DEBUG

so the whole thing goes out of debug mode.

or i have to do it everytime pre-release

Tuesday, April 07, 2009 1:01 PM by naysh (blackvine@gmail.com)

# re: Is my C# application running in debug mode? or: About pre-processor directives

try

if (Debugger.IsAttached)

 ...

Monday, July 27, 2009 9:27 PM by Chris

# re: Is my C# application running in debug mode? or: About pre-processor directives

nice job. thanks

Saturday, August 22, 2009 2:00 AM by sami

# re: Is my C# application running in debug mode? or: About pre-processor directives

It saved me much time. Thanks.

Wednesday, September 16, 2009 8:22 AM by robossliu

# re: Is my C# application running in debug mode? or: About pre-processor directives

Found this useful. Thanks for sharing.

Wednesday, December 30, 2009 4:30 PM by Giammarco Schisani

# Debugging output

This article:

blogs.msdn.com/.../How-do-I-send-out-simple-debug-messages-to-help-with-my-debugging_3F00_.aspx

discusses what you're talking about briefly, but also offers advice on what to do if you just want to output debugging messages.

Great post! Thanks!

Friday, January 22, 2010 2:35 AM by Fabs

# re: Is my C# application running in debug mode? or: About pre-processor directives

Thanks!

Wednesday, June 02, 2010 6:45 AM by AVEbrahimi

# re: Is my C# application running in debug mode? or: About pre-processor directives

Awesome post.

it saved my time.

Saturday, February 18, 2012 1:25 PM by Ivan Lewis

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 8 and 8 and type the answer here: