Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Access Automation in Console Projects

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

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

  1. Start Visual Studio and create a new console application in either Visual Basic or Visual C#.

  2. Add references to EnvDTE, EnvDTE80, EnvDTE90, andEnvDTE100.

  3. Copy the following code into the project's module or program class, respectively.

  4. Build and run the project.

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.

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);
        }
    }
}
Show:
© 2015 Microsoft