vb.net 测试代码是否在调试模式下执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1265581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing if code executes in debug mode
提问by OrElse
How can i test that the code executes in debug mode.
我如何测试代码在调试模式下执行。
Here is what i would like to do in pseudocode
这是我想用伪代码做的事情
if not debugMode then
Do something()
end if
回答by MarkJ
You can use Debugger.IsAttachedto determine whether the program is being debugged.
您可以使用Debugger.IsAttached来确定程序是否正在被调试。
If Not Debugger.IsAttached Then
DoSomething()
End If
EDIT If you always want to skip the DoSomething
code in the debug build, whether or not a debugger is being used, use conditional compilationwith #If, something like this
编辑如果您总是想跳过DoSomething
调试版本中的代码,无论是否使用调试器,请使用带有#If 的条件编译,类似这样
#IF DEBUG Then
DoSomething()
#End If
回答by Fredrik M?rk
What do you mean with debug mode? If you refer to a debug build, you can use #if DEBUG
to test for that:
调试模式是什么意思?如果您指的是调试版本,则可以使用它#if DEBUG
来测试:
#if DEBUG
// this is included in a debug build
#else
// this is not included in a debug build
#endif
回答by Wael Dalloul
you can use IsDebuggerPresent Function
您可以使用 IsDebuggerPresent 函数
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function IsDebuggerPresent() As Boolean
End Function
if not isDebuggerPresent() then
Do something()
end if