How to: Access Automation in Console Projects
Automation members are not used exclusively in add-ins, macros, or wizards. You can use them with any Visual Studio project type. The following procedure demonstrates how to reference and use automation objects and members within a console project.
To use automation in a console application
Start Visual Studio and create a new console application in either Visual Basic or Visual C#.
Add references to EnvDTE, EnvDTE80, EnvDTE90, andEnvDTE100.
Copy the following code into the project's module or program class, respectively.
Build and run the project.
Example
The following example demonstrates how to obtain an instance of Visual Studio, get a reference to automation's DTE2 object, and then use it to access the Solution2 object.
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE100
Module Module1
Sub Main()
Dim dte As EnvDTE80.DTE2
Dim obj As Object = Nothing
Dim t As System.Type
t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0", True)
obj = System.Activator.CreateInstance(t, True)
dte = CType(obj, EnvDTE80.DTE2)
Dim soln As Solution2 = CType(dte.Solution, Solution2)
MsgBox(soln.Count)
End Sub
End Module
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
Using EnvDTE100;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
EnvDTE80.DTE2 dte;
object obj = null;
System.Type t = null;
// Get ProgID for DTE 8.0
// NOTE: Do not put this call in a try block because it
// will cause Visual Studio to fail to respond.
t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0",
true);
// Attempt to create an instance of envDTE.
obj = System.Activator.CreateInstance(t, true);
// Cast to DTE2.
dte = (EnvDTE80.DTE2)obj;
// Get a reference to the solution2 object.
Solution2 soln = (Solution2)dte.Solution;
// Now you can do what you like with it.
System.Windows.Forms.MessageBox.Show
("Solution count: " + soln.Count);
}
}
}