Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Visual Studio how to add special cases for debug and release versions? [duplicate]

Possible Duplicate:
C# if/then directives for debug vs release

I am working on a C# project and I like to know how to add special cases while I am running the program in the debugger in the Debug Mode I have access to certain resources that I would not normally have in the release version.

Here is what keeps happening, I have admin tools built into the program, that are just for debugging, like a button that says test and put what ever code I want to into it. What keeps happening I forget to hide that button and I release it to the clients. I would love to have that test button there only while it's running in the debug mode but not any other time.

This turns on my admin tools

rpg_Admin.Visible = true;

This turns off my admin tools

rpg_Admin.Visible = false;

Is there a simple way to do this?

if Debug Mode
    rpg_Admin.Visible = true

or maybe while it's running in visual studio it's

rpg_Admin.Visible = true

but when it's running on it's own

rpg_Admin.Visible = false

I am running on Visual Studio 2010

Thanks.

like image 335
David Eaton Avatar asked Dec 27 '12 14:12

David Eaton


2 Answers

Using your example, add some #if / #else / #endif directives like so:

#if DEBUG
    rpg_Admin.Visible = true;
#else
    rpg_Admin.Visible = false;
#endif

You could also apply System.Diagnostics.ConditionalAttribute attributes to any code that's only used in the debug version. That will completely remove any unnecessary code from the release build. Example:

[Conditional("DEBUG")]
public static void MyDebugOnlyMethod()
    {
    }
like image 136
Tim Long Avatar answered Oct 24 '22 15:10

Tim Long


wrap the debug code in #ifdef DEBUG/#endif:

#ifdef DEBUG

// debug only code here

#endif
like image 34
Z.D. Avatar answered Oct 24 '22 15:10

Z.D.