C# 调试和发布模式

4

如何判断是在调试模式还是发布模式?是否有其他方法可以找到?

    #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

6
那是通常的方式,你还有其他想法吗? - Kevin DiTraglia
2
请参阅http://blogs.msdn.com/b/ericlippert/archive/2009/09/10/what-s-the-difference-between-conditional-compilation-and-the-conditional-attribute.aspx,了解有关条件编译和条件属性之间的区别。 - Eric Lippert
2个回答

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");

@Kevin DiTraglia 另一种方法是:我们可以使用 [Conditional("DEBUG")] 来进行调试模式。 - Dev
就像你所说,那是唯一的方法:对于调试模式,我们能不能不使用 [Conditional("DEBUG")]? - Dev
@Rashmi Eric Lippert已经对ConditionalAttributes发表了评论。你有没有阅读他的链接和博客?你的问题并不是很清楚你想要实现什么...你为什么认为你展示的方法不够用呢?问题应该集中在你面临的实际问题上,它是什么问题? - Alan

1
你可以尝试使用System.Diagnostics: < p >如果 (Debugger.IsAttached) {...

?

2
这个检查是否有调试器附加,可能发生在调试模式或发布模式下。 - Ahmed KRAIEM

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