Detecting if a program was run by Visual Studio, as opposed to run from Windows Explorer

后端 未结 6 1375
萌比男神i
萌比男神i 2020-12-14 15:25

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

Our software has a bug reporting fea

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 15:56

    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();
        }
    }
    

提交回复
热议问题