This documentation is archived and is not being maintained.

How to: Create and Attach to Another Instance of Visual Studio

In some cases it is useful to programmatically create a new instance of Visual Studio or attach to a specific instance of Visual Studio that is already running. If two instances of Visual Studio are running on a system and both have the same solution open — for example, one instance is performing a solution build and the other is perform a debug build — you can program your add-in to differentiate between them.

You can, for example:

  • Start an instance of Visual Studio based on a path to a file or solution.

  • Attach to an instance of Visual Studio based on a path to a file or solution.

  • Load a file or solution into an existing instance of Visual Studio.

  • Create a new instance of Visual Studio where the instance shuts down when:

    • The external reference count on both the DTE object and the Solution object are 0.

      -and-

    • The integrated development environment (IDE) is not displayed to the user or is not under user control.

  • Create a new instance of Visual Studio where the instance remains loaded even when:

    • The external reference count on both the DTE object and the Solution object are 0.

    • The IDE is displayed to the user.

      -and-

    • The IDE is under user control.

The returned objects can be cast to their respective objects, such as DTE2 and Solution2.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create a new instance of Visual Studio, use System.Activator.CreateInstance. The example below illustrates this method.

public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    CreateNewInstance2(_applicationObject);
}

private void CreateNewInstance2(DTE2 dte)
{
    System.Type Type;
    object inst;
    EnvDTE80.DTE2 dte80Obj;
    Type = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
    inst = System.Activator.CreateInstance(Type, true);
    dte80Obj = (EnvDTE80.DTE2)inst;
    System.Windows.Forms.MessageBox.Show
      (dte80Obj.DisplayMode.ToString());
}
// Create a new instance of Visual Studio by using 
// GetTypeFromProgID and CreateInstance.
private void CreateNewInstance1()
{
    System.Type type = System.Type.GetTypeFromProgID
    ("VisualStudio.DTE.10.0");
    Object obj = System.Activator.CreateInstance(type, true);
    EnvDTE80.DTE2 dte8Obj = (EnvDTE80.DTE2)obj;
}

In the Visual Basic example, both statements create a new instance of the Visual Studio IDE. The first statement directly creates a new instance while the second statement creates a new instance by creating a new solution.

Show: