如果检测程序是由Visual Studio中运行,而不是从Windows Explorer中运行 [英] Detecting if a program was run by Visual Studio, as opposed to run from Windows Explorer

查看:125
本文介绍了如果检测程序是由Visual Studio中运行,而不是从Windows Explorer中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,如果你的程序通过Visual Studio加载与它是否开始作为一个独立的可执行文件来检测?

Is there a way to detect if your program was loaded through Visual Studio vs. whether it was started as a standalone executable?

我们的软件有一个错误报告功能处理未处理的异常 - 我们需要能够分发调试版本给我们的beta测试,但我们不希望bug报告熄灭,当我们在开发过程中,由于异常是很多,如果更有益VS捉住他们一个完整的堆栈跟踪等。

Our software has a bug reporting feature to handle unhandled exceptions -- we need to be able to distribute debug builds to our beta testers, but we don't want the bug report to go off when we are in the middle of development, because the Exceptions are a lot more useful if VS catches them with a full stack trace, etc.

现在,我禁用错误报告,如果 Application.ExecutablePath 包括bin\Debug或bin\Release,但我想有可能是检测程序是否通过加载VS一种更可靠的方法。

Right now, I'm disabling the bug report if Application.ExecutablePath includes bin\Debug or bin\Release, but I figure there is probably a more robust way of detecting whether the program was loaded through VS.

显然,我们可以建立一个不同的构建与一些预处理宏,但对这个问题的缘故,假设的可能性也不大 - 我不介意添加代码,但我试图进行修改,以最少的建设过程中,这就是为什么命令行选项是一种不得已而为之的为好。

Obviously, we could set up a different build with some preprocessor macros, but for the sake of the question, assume that isn't a possibility -- I don't mind adding code, but I'm trying to make the fewest modifications to the build process, which is why command-line options are kind of a last resort as well.

如果它的事项,我使用VS2003 / .NET 1.1。

If it matters, I'm using VS2003/.NET 1.1.

推荐答案

如果你这样做,以确定它是否是在任何调试器(由澄清的 @JaredPar 的),你可以使用 Debugger.IsAttached 在异常处理程序。

If you're doing this to determine if it is in any debugger (clarified by @JaredPar), you can use Debugger.IsAttached in the exception handler.

try
{
    // ...
}
catch(Exception ex)
{
    if (!Debugger.IsAttached)
    {
        ExceptionHandler.Frob(ex);
    }
    else
    {
        throw;
    }
}



或者

Alternatively:

public static void Frob(Exception ex)
{
    if (Debugger.IsAttached)
    {
        Debugger.Break();
    }
}

这篇关于如果检测程序是由Visual Studio中运行,而不是从Windows Explorer中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆