C#如何判断程序处于调试状态?

如题所述

看上面的图标


没有调试

正在调试


温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-20
  1.通过#if预编译指令对DEBUG进行判断,如下:
  
#if DEBUG
// 调试用代码
……
……
#endif

  2.DotNet程序的调试,是DotNet程序员必备的技能之一,开发出稳定的程序、解决程序的疑难杂症都需要很强大的调试能力。DotNet调试有很多方法和技巧。现在本文就介绍一下借助DebugView工具进行调试的方法,以及由DebugView引申出来的知识点。

  3.C#判断是否运行在调试模式下

  很多情况下我们希望一些调试信息不输出,但又不至于用到trace和debug的一些功能,仅仅是包一下几句话,非调试状态就不运行,有这些用法

using
System.Diagnostics;

class
XY

{

[Conditional(
"DEBUG ")]

public
static
void
DebugLog(string
in_string)

{

Console.WriteLine(in_srting);

}

public
static
int
Main(string[]
in_args)

{

DebugLog(
"This
is
a test ");

return
5;

}

}

if
(System.Environment.StackTrace.ToLower().IndexOf(
":line ")> =0)

Console.WriteLine( "debug ");

else

Console.WriteLine( "release ");

string
buildtype;

try

{

bool
found
=
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(DebuggableAttribute),
false).Length
>
0;

buildType
=
found
? "Debug "
:
"Release ";

}

catch

{

buildType
= " <error>
";

}

#If
DEBUG
Then

'调试状态下运行

Else

#End
If
第2个回答  2013-05-14
#if DEBUG
//这里的代码在 DEBUG 模式下编译
#else

//这里在非 DEBUG 模式下编译
#endif追问

能否具体点呢?谢谢!

追答

这个... 还怎么具体...
中间的代码就是你想怎么写就则么写,
#if DEBUG
这里就是普通的 C# 代码。
比如你要在 DEBUG 状态下,登录窗口默认输入你的登录帐号。
那就写
textBox1.Text ="帐号名";
textBox2.Text ="密码";
#endif

这样就行了,编译器会在 DEBUG 模式下,将你中间那两行编译,如果在 Release 状态下,中间那两行赋值代码将不会被编译。

本回答被提问者采纳
第3个回答  2013-05-14
你按F5默认就是调试模式!设置断点就可以调试了!
相似回答