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

后端 未结 6 1376
萌比男神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:55

    Instead of tracking by process tree, I would add a configuration flag that enables the reporting feature. The flag can always default to "true" unless you are in your DEV environment then you set it to "false".

    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 16:02

    Have you considered command line arguments? Run the program from Visual Studio with a --no-exception-handling flag (or whatever sounds appropriate), and don't handle exceptions if that argument is passed in. When you start the program elsewhere, without this argument, it'll behave normally.

    0 讨论(0)
  • 2020-12-14 16:02

    Sometimes the application is started outside the debugger and the debugger gets attached later. (Doubleclick on a file where the application is assigned to ...) I use this code to wait for the debugger attach.

    using System.Diagnostics;
    
    Process[] procName = Process.GetProcessesByName("devenv");
    
    if(procName.Length > 0)
      MessageBox.Show("Wait for debugger attach");
    
    0 讨论(0)
  • 2020-12-14 16:04

    I know this is old but the provided solutions are not very satisfying.

    I used the following class instead:

    using System.IO;
    using System.Reflection;
    
    public static class Program
    {
        public static string ExecutablePath
        {
            get;
            private set;
        }
    
        static Program()
        {
            var assemblyPath = Assembly.GetEntryAssembly().Location;
            var assemblyDirectory = Path.GetDirectoryName(assemblyPath);
    
            if (assemblyDirectory.EndsWith(@"\Debug") || assemblyDirectory.EndsWith(@"\Release"))
            {
                string projectFile = Path.GetFileNameWithoutExtension(assemblyPath) + ".csproj";
    
                var root = new DirectoryInfo(assemblyDirectory);
    
                while (root.Parent != null)
                {
                    if (File.Exists(Path.Combine(root.FullName, projectFile)))
                        break;
    
                    root = root.Parent;
    
                    if (root.Parent == null) // we could not find it (should not happen)
                        ExecutablePath = assemblyDirectory;
                }
    
                ExecutablePath = root.FullName;
            }
            else
            {
                ExecutablePath = assemblyDirectory;
            }
        }
    }
    

    Then you can just use Program.ExecutablePath. If you already have a class named Program you can just extend it by those properties and methods.

    If running from Visual Studio it will give you the project path where the csproj-file resides. This is the executable path without the "bin\*\Debug" or "bin\*\Release" stuff.

    If not running from Visual Studio it will give you the path where the executable resides.

    The solution is independent of debug settings, other attached debuggers or build configurations. The only important thing is, that your configurations are named "Release" and "Debug".

    Note: As Troy Gizzi mentioned in the comments, this solution only works if you run the executable from another directory than the output directory. For my use case (simulate the deployment directory structure with the project directory as the root directory), this is a suitable solution. In general I copy my executable later to the deployment directory and expect the same behavior as if I run my program from within Visual Studio. Content and other dependencies are located relative to the project directory in my case.

    0 讨论(0)
  • 2020-12-14 16:09

    I don't do .net development, but in java I have done this by passing a flag into the startup options of the application. So you could pass a debug flag into the app from the IDE, and then check for that, when the app is run as an executable the flag would not be present. I would be surprised if .net didn't have something similar.

    0 讨论(0)
提交回复
热议问题