C#调试和发布模式

时间:2013-09-27 14:56:29

标签: c#

如何查找是处于调试模式还是发布模式?有没有其他方法可以找到它?

    #if(DEBUG)
{
       Console.WriteLine("Debug mode");
       //Or Things to do in debug mode
}
    #else
{
       Console.WriteLine("Release mode");
       //Or Things to do in Release mode- May be to change the text, image 
}
#endif

2 个答案:

答案 0 :(得分:6)

不,这是唯一的方法,但您需要正确的语法和大小写。您还可以检查调试器是否已附加。这是正确的语法:

#if DEBUG
    Console.Writeline("debug");
#else
    Console.Writeline("release");
#endif
    // You can also check if a debugger is attached, which can happen in either debug or release
    if (Debugger.IsAttached)
        Console.WriteLine("debugger attached");

答案 1 :(得分:1)

您可以尝试使用System.Diagnostics:

if (Debugger.IsAttached) {...