Is my C# application running in debug mode? or: About pre-processor directives
By rickvdbosch
- 1 minutes read - 206 wordsAll 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