检测代码是否在调试模式下执行

17

如何测试代码是否在调试模式下运行。

以下是我想要用伪代码实现的内容

if not debugMode then
  Do something()
end if
3个回答

31

你可以使用Debugger.IsAttached来判断程序是否正在被调试。

If Not Debugger.IsAttached Then
  DoSomething()
End If

编辑:如果您始终希望在调试构建中跳过DoSomething代码,无论是否使用调试器,请使用条件编译#If,类似于这样的方式。

#IF DEBUG Then
  DoSomething()
#End If

10

您所说的调试模式是指什么?如果您指的是调试构建(debug build),您可以使用#if DEBUG进行测试:

#if DEBUG
    // this is included in a debug build
#else
    // this is not included in a debug build
#endif

1

你可以使用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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接