How to detect that C# Windows Forms code is executed within Visual Studio?

后端 未结 8 1919
春和景丽
春和景丽 2020-11-29 23:46

Is there a variable or a preprocessor constant that allows to know that the code is executed within the context of Visual Studio?

相关标签:
8条回答
  • 2020-11-30 00:13

    I think the simplest and most reliable way to determine if your extension is executed in the WinForms designer is to check the current process.

    public static bool InVisualStudio() {
      return StringComparer.OrdinalIgnoreCase.Equals(
        "devenv", 
        Process.CurrentProcess.ProcessName);
    }
    
    0 讨论(0)
  • 2020-11-30 00:14

    There is the DesignMode property for Components. It is handy when you use the Design Viewer of VS.

    But when you talk about debugging in Visual Studio you need to use the Debugger.IsAttached property. Then, you can use

    #if DEBUG
    #endif
    

    too

    0 讨论(0)
  • 2020-11-30 00:15

    The DesignMode property isn't always accurate. We have had use this method so that it works consistently:

        protected new bool DesignMode
        {
            get
            {
                if (base.DesignMode)
                    return true;
    
                return LicenseManager.UsageMode == LicenseUsageMode.Designtime;
            }
        }
    

    The context of your call is important. We've had DesignMode return false in the IDE if running in an event under certain circumstances.

    0 讨论(0)
  • 2020-11-30 00:19

    I use this extension method:

    internal static class ControlExtension
    {
        public static bool IsInDesignMode(this Control control)
        {
            while (control != null)
            {
                if (control.Site != null && control.Site.DesignMode)
                    return true;
                control = control.Parent;
            }
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 00:25

    There is a DesignMode property that you can check but in my experience it's not always accurate. You could also check to see if the executable is DevEnv.exe

    Take a look here. Might make this question a dup but it all depends on what you're trying to accomplish.

    0 讨论(0)
  • 2020-11-30 00:30

    Try Debugger.IsAttached or DesignMode property or get ProcessName or a combination, as appropriate

    Debugger.IsAttached // or                                       
    LicenseUsageMode.Designtime // or 
    System.Diagnostics.Process.GetCurrentProcess().ProcessName
    

    Here is a sample

    public static class DesignTimeHelper {
        public static bool IsInDesignMode {
            get {
                bool isInDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime || Debugger.IsAttached == true;
    
                if (!isInDesignMode) {
                    using (var process = Process.GetCurrentProcess()) {
                        return process.ProcessName.ToLowerInvariant().Contains("devenv");
                    }
                }
    
                return isInDesignMode;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题