HOWTO: Guessing the IDE mode (design, debug or run-time) from a Visual Studio add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: March 2007   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012


The IDE can be in one of these modes:

  • Design-time: this is the mode that you use to code or design your application.
  • Run-time: this is the mode when your application is running.
  • Debug-time: this is the mode when you debug your running application.

More Information

When you want to know programatically the mode of the IDE, you could think that you can use the EnvDTE.DTE.Mode property. However, the EnvDTE.vsIDEMode enum returned by that property has only two states (vsIDEModeDebug, vsIDEModeDesign), not three. Fortunately, you can use the EnvDTE.DebuggerEvents class, that provides the OnEnterDesignMode, OnEnterBreakMode and OnEnterRunMode events to track the current mode, assuming that the IDE is initially at design-time. So, you can use this code:

Public Class Connect
   Implements IDTExtensibility2
   Private Enum IDEMode
      Design = 1
      Debug = 2
      Run = 3
   End Enum
   Private _applicationObject As DTE2
   Private _IDEMode As IDEMode
   Private WithEvents _debuggerEvents As DebuggerEvents
   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, _
       ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection

      _applicationObject = CType(application, DTE2)
      _IDEMode = IDEMode.Design
      _debuggerEvents = _applicationObject.Events.DebuggerEvents

   End Sub
   Private Sub _debuggerEvents_OnEnterBreakMode(ByVal Reason As EnvDTE.dbgEventReason, _
      ByRef ExecutionAction As EnvDTE.dbgExecutionAction) Handles _debuggerEvents.OnEnterBreakMode

      _IDEMode = IDEMode.Debug

   End Sub
   Private Sub _debuggerEvents_OnEnterDesignMode(ByVal Reason As EnvDTE.dbgEventReason) _
      Handles _debuggerEvents.OnEnterDesignMode

      _IDEMode = IDEMode.Design

   End Sub
   Private Sub _debuggerEvents_OnEnterRunMode(ByVal Reason As EnvDTE.dbgEventReason) _
      Handles _debuggerEvents.OnEnterRunMode

      _IDEMode = IDEMode.Run

   End Sub
End Class

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this