Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Release/Debug mode in code

Status
Not open for further replies.

CosmicCharlie

Programmer
Jun 30, 2006
44
US
I need a way to determine if my application is in debug mode or release mode in code. I need this so I can get a different connection string from a config file when I am in debug mode than when I am in release. Does anyone know how to do this or have any related recommendations?

Thank you.

Cosmic Charlie
 
#IF DEBUG Then
'do something in Debug mode
#Else
'do something else in Release mode
#End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thank you, both. I knew it was easy, I just didn't know how.

To be on the safe side, I have implemented my code this way:

Code:
            m_strCnStrings = m_colCnStrings.Get("LiveConnString").ToString
#If DEBUG Then
            m_strCnStrings = m_colCnStrings.Get("TestConnString").ToString
#End If

That way, the Release code is compiled no matter what, and is overridden only in debug mode. This is, of course, slightly less efficient, but then only in debug mode, so my users won't care.

CC
 
If you want to evaluate at runtime, you can use:
Code:
If System.Diagnostics.Debugger.IsAttached Then

    'Debug

Else

    'Release

End If
 
Thanks for the suggestion. It appears poorly documented by Microsoft. Can you tell me how this differs from using the compiler option?

CC
 
#IF is a compiler directive, it will only compile when the build mode is set to Debug.

System.Diagnostics.Debugger.IsAttached is a run-time check to determine if there is a debugger attached. I would be careful using it in a check for the build version as a debug build can be executed with out a debugger attached.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Something else to consider.
I use File= to get appSettings that change between release types.
I use
<appSettings file="webTest.config">
or
<appSettings file="webProd.config">
in the web.config to point to the file that contains the appSettings that are different.

Yes, I have to change web.config depending on the release.

- free online Compare/Diff of snippets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top