是否有一个等同于IsDebuggerPresent()的C#?

noe*_*cus 15 c#

我确实找到了这段代码片段,但在调试时它并没有返回true:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
internal static extern bool IsDebuggerPresent();
Run Code Online (Sandbox Code Playgroud)

Bug*_*der 33

是:

System.Diagnostics.Debugger.IsAttached
Run Code Online (Sandbox Code Playgroud)


Chr*_*n.K 5

你标记了你的问题,C#所以我在说"但是当我调试时它没有返回真实",你实际上在谈论托管调试.

IsDebuggerPresent()函数检查是否存在本机调试器.在您的情况下,您应该使用System.Diagnostics.Debugger.IsAttached检查是否存在(托管)调试器.

有关调试器API的更多信息,另请参阅此博客文章.