How to tell "if on development environment"? - c#

I'd like to add a condition to my C# program i'm develop in Visual Studio:
#If onEditor
do something
In Unity exists if Application.isEditor
Does exists something in 'regular' (WinForms or ASP.NET) C# application on Visual Studio?

#if are compile directives, so whatever goes there will be checked on compile time, and not on runtime. There is #if DEBUG which effectively means 'this was build in debug mode', rather than 'release mode'. It doesn't tell anything about the origin of the running if your program.
I think what you are looking for is Debugger.IsAttached: it checks if a debugger is attached. If that it true, the program is either ran from Visual Studio, or a debugger was attached later on.

Use this in Visual Studio to test if you're running in Debug mode:
if (Debugger.IsAttached)
{
Debugger.Break();
}
Use #if DEBUG to conditionally compile code in - either debug mode or release mode.

You can use:
#if DEBUG
DEBUG constant is defined for Debug configuration in all default Visual Studio project templates.

Related

#if DEBUG doesn't seem to work in my VS 2010

I've been doing the following in my C# code:
#if DEBUG
//Debugger only code
#endif
But for some reason when I do the same in my C# console application (that is built as a Windows service in VS 2010) it doesn't seem to work and for both Release and Debug configurations it looks like DEBUG is not defined.
Do I need to set something for that preprocessor constant to work?
When using the 'Debug' configuration, you have to go to project's Properties -> Build
and tick the 'Define DEBUG constant' box.
Either that, or define the conditional compilation symbol: DEBUG. As you can do with any other custom compilation symbol :)

c# Production code running DEBUG version

I have the following code:
#if (DEBUG)
imgPath = GetDirectoryName(Application.ExecutablePath);
#else
imgPath = GetDirectoryName(Application.ExecutablePath) + "\\images\\";
#endif
When the code went into Production (live site) , it still looked the the DEBUG version. How is this possible? Is there something during the promotion process that can indicate do RELEASE vs DEBUG
You might have deployed a version with binaries taken from a Debug build configuration or in any case, the DEBUG variable set to True.
You need to build in RELEASE and use that output as the release candidate.
When using #if statements, it is checking to determine if the constant is defined. Under the project's properties->Build section, the DEBUG constant is (by default) set to be defined when in debug mode. There is no RELEASE constant unless you define it yourself. You may have used your build from debug mode or had the "Define DEBUG constant" checked for your release build. Double check which you used and your build settings.
Also, your code should be greyed out to indicate that VS will not be compiling that code. So if the code you wish to be using is grey, that is an indicator you have DEBUG defined somewhere.

How to leave certain lines out of build using visual studio?

I am new to C# and Visual Studio, and this may be a silly question but I do have to find an answer. My question is is it possible to differentiate debug and release builds in such a way the certain lines of code(in my case MessageBox.Show(...)) are compiled if I build under Debug mode, but are left out if I build under Release mode?
Wrap the lines of code in #if DEBUG statements:
#if DEBUG
MessageBox.Show("Message");
#endif
If you want to do different things in release then you can have:
#if DEBUG
MessageBox.Show("Message");
#else
// Write to log file
#endif
Source
you can use the compiler instruction "#if DEBUG" followed by "#endif" so your code will be compiled only on debug mode.
find more details here:
http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
In addition to #if DEBUG, you can also use the Assert static method on the Debug class in System.Diagnostics to check things and show a message box if the value is wrong. e.g:
Debug.Assert(IsValid);
Will show a message in the debug build if IsValid is false.
You can specify different conditional compilation symbols in the project's properties (an article here; MSDN documentation for Visual Studio). Then, using conditional compilation it's easy to include/exclude certain lines. Like this:
#if DEBUG
MessageBox.Show(...);
#end
Yes, you can use
#if DEBUG
MessageBox.Show....
#endif
If you check build version, you find you have variables defined in project: DEBUG is only defined in debug release.
If you want you can also switch the behaviour using
#if DEBUG
MessageBox.Show....
#else
// do something else here in release mode
#endif

#If DEBUG is ignored (VB.net or C#)

I have several of these in my code which have been working fine so far:
#If DEBUG Then
... some code here
#End If
Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If"
gets execute also in "Release Mode".
This is strange and did not happen before. What could have happened so that
the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?
I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.
-Pam
Firstly, make sure you understand the difference between how you're running the code and how you're building it. Too many people equate "launching in a debugger" with "the debug version" and "launching not in a debugger" with "the release version". They're completely orthogonal - you can launch a release build in a debugger (typically with less information available) and you can launch a debug build not in a debugger. Apologies if you were already aware of this.
Now, assuming you really have changed the project configuration you're building to Release, you need to check the project properties for that specific configuration. I don't know what it looks like in VB, but in C# in the project properties, in the build tab, there will be a list of defined symbols - that is what affects whether #if DEBUG code is built or not. Perhaps someone has copied over the project configuration from Debug into Release?
EDIT: One way to check this at build time is:
#if DEBUG
#error This shouldn't happen
#endif
In a release build, that should build without error. In debug, it won't.
EDIT: Another option is that your overall solution configuration is now referring to the wrong project configuration types. I can't remember the exact menu name, but if you look around Project for Configuration Manager, you should be able to bring up a grid mapping "Project" and "Solution Configuration" to the project configuration to build.
C# Project ( Visual Studio )
go to: Project Properties -> Build(tab)
Select Configuration: Release
Uncheck "Define DEBUG constant"
Now select Configuration: Debug
Check "Define DEBUG constant"
In your code, you can now type the following ( DEBUG with uppercase )
#IF DEBUG
// Debugging code goes here
#ENDIF
Under Project Properties / Compile / Advanced Compile Options there is
a checkbox called "Define Debug Constant" that sets this.
Check out: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24658238.html
EDIT: Try this initializing with this:
#If CONFIG = "Debug" Then
#CONST DEBUG = true
#if CONFIG = "Release" Then
#CONST DEBUG = false
Did you, by any chance, tick the "Define DEBUG constant" for the Release configuration, while you were in the Project Properties / Build?
Also make sure you are not building the project-level Debug configuration within the solution-level Release configuration (see the Configuration Manager).
Also remember #if DEBUG must be in uppercase. e.g. #if debug won't work.
Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.
My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out.
This seems to be a possible bug in VS 2019 (16.4.5)?
undefine DEBUG and that will not execute that portion.
If you are using ASP.NET make sure about this line in Web.Config file:
<compilation debug="false" targetFramework="4.5">
So if debug="true" your project runs in DEBUG mode.
If you are using VB.NETFramework v4.5 then use like
If Debugger.IsAttached Then
'... some code here
End If

Pre-Processor directives in C#

I seem to be having trouble with preprocessor directives in C#. I've created a Visual Studio 2008 C# win forms app. I add this:
#if (DEBUG)
textBox1.Text = "in debug mode";
#else
textBox1.Text = "in release mode";
#endif
And when I run in debug I see the expected "in debug mode". However when I switch to Release, compile, and run the .exe, I still see the "in debug mode" text. In my project properties I have Define DEBUG constant checked. I even get the correct color-coded syntax for the code above. What gives?
Any chance you have DEBUG defined for both Debug and Release configurations?
Do you have a "#define DEBUG" line ? That sets DEBUG to true always.

Categories

Resources