Mawg
Mawg

Reputation: 40140

How to tell if (VB) code is running inside Visual Studio?

I'm using VB Express 2008. I know how to tell if code was built in debug more or release, but now I would like know how to tell if it is running in the Visual Studio IDE or not (so that I can add some debug MsgBox() and not worry about them showing if I ever accidentally ship a debug version).

Upvotes: 0

Views: 470

Answers (4)

StarPilot
StarPilot

Reputation: 2272

If you want to make sure you never show debug messages to users, you can use Debug.Write() and Debug.WriteLine(). These commands will output the text supplied to the debug output window. Note that you can attack a debug output window to a program running production code on a customer's machine without installing a development environment!

Testing for an attached debugger does not indicate that the debugger is also inside an IDE. It is quite common in many environments to attach debuggers to production code running on a customer system to identify what is going wrong on a particular customer's installation and usage. Testing for a debugger and then presuming you are in an IDE will foul out this usage of debuggers in a production environment.

Upvotes: 1

Jorgen Thelin
Jorgen Thelin

Reputation: 1076

Try checking the IsAttached property of System.Diagnostics.Debugger

Upvotes: 1

Guy
Guy

Reputation: 320

Is System.Diagnostics.Debugger.IsAttached what you're looking for?

Upvotes: 1

p.campbell
p.campbell

Reputation: 100567

If you're building for Test and Prod, consider using a preprocessor directive in your code.

#If DEBUG Then
    MsgBox("Foo")
#End If

This falls down, of course, if you ship a debug-built binary to a non-dev environment. I understand this is attacking the problem from another angle from where you asked the question (the IDE).

Upvotes: 1

Related Questions