![]() |
![]() |
|||
![]() |
||||
![]() |
![]() |
This article describes how to automate the Visual Studio .NET IDE from outside the IDE. More Information Visual Studio .NET exposes an extensibility model that can be used to automate it. This object model resides in the EnvDTE.dll assembly, which you can explore using the Object Browser. The root class of the object model is EnvDTE.DTE, and therefore you need an instance of this class to automate it. You have the following ways to automate the Visual Studio .NET IDE:
The previous methods automate the IDE from within, but there is a third method that you can use to automate the IDE from the outside, for example from other application or from a script. To do this, first you need to create an instance of the EnvDTE.DTE class. This is done using the CreateObject function of most COM-aware languages, passing the ProgID of the class. The list of available ProgIDs is the following:
If you want to use late binding (for example, automating Visual Studio in VBScript) you just use the object returned by the CreateObject function. If you want to use early binding (for example, automating Visual Studio in a .NET application), then you need to cast the object returned by the CreateObject function to the EnvDTE.DTE type. To do this, your project needs to add a reference to the EnvDTE.dll assembly. Notice that there are two versions of the EnvDTE.dll assembly:
As a consequence, using early binding you can't automate all Visual Studio versions at the same time, because even if your .NET application uses .NET 1.0 to run on all .NET Frameworks, if it references EnvDTE.dll version 7.0.3300.0 it won't work on systems with only Visual Studio 2005 or higher installed; and if it references EnvDTE.dll version 8.0.0.0 it won't work on systems with only Visual Studio .NET 2002 or 2003 installed. There are two important properties that control the behavior of the IDE while you are automating it from the outside:
The following VBScript sample shows how to create an instance of Visual Studio .NET 2003 and show its name and version: Dim objDTE ' Creates an instance of the Visual Studio .NET 2003 IDE Set objDTE = CreateObject("VisualStudio.DTE.7.1") ' While the instance is still invisible, show its name and version MsgBox objDTE.Name & " " & objDTE.Version ' Make it visible and keep it open after we finish this script objDTE.MainWindow.Visible = True objDTE.UserControl = True The following VB.NET sample does the same: Dim objType As Type Dim objDTE As EnvDTE.DTE ' Creates an instance of the Visual Studio .NET 2003 IDE objType = Type.GetTypeFromProgID("VisualStudio.DTE.7.1") objDTE = DirectCast(System.Activator.CreateInstance(objType), EnvDTE.DTE) ' While the instance is still invisible, show its name and version MsgBox objDTE.Name & " " & objDTE.Version ' Make it visible and keep it open after we finish this script objDTE.MainWindow.Visible = True objDTE.UserControl = True Follow @VSExtensibility |
Copyright © 2000-2021 MZTools Software. All Rights Reserved. Legal Notice |