开发者

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 accid开发者_JS百科entally ship a debug version).


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


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).


Try checking the IsAttached property of System.Diagnostics.Debugger


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜