How to start Visual Studio programmatically

One of the ways we test Visual Studio is by automating the devenv.exe process using a library called DTE (Design Time Extensibility). To use this library from your .NET application, you’ll need to add a reference to the EnvDTE assembly (which is usually available on the .NET tab of the Add Reference dialog).

Starting Visual Studio using DTE

Here's a simple code snippet that starts Visual Studio and displays its main window:

 using System;
using EnvDTE; 

class Program
{
    static void Main(string[] args)
    {
        Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.9.0");
        DTE dte = Activator.CreateInstance(visualStudioType) as DTE;
        dte.MainWindow.Visible = true;
    }
}

When the VS object is being created, a VS process (devenv.exe) starts in the background. You can make its main window visible using dte.MainWindow.Visible = true;

Note that when the parent process (your program) ends, VS will close with it as well.

To get an instance of an already running VS process, you can use the following snippet:

 EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)
    System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0");

This snippet also demonstrates using DTE2, a newer version of the DTE interface that provides additional functionality.

DTE interface

Since DTE is COM based, we need to get the type that represents DTE from a well-known ProgID (“VisualStudio.DTE.9.0” that can be found in the registry). Once we have that type, we create an instance of it using Activator and cast it to the DTE interface. Contrary to what the name suggests, DTE is actually an interface and not a class:

 namespace EnvDTE
{
    [CoClass(typeof(DTEClass))]
    [Guid("04A72314-32E9-48E2-9B87-A63603454F3E")]
    public interface DTE : _DTE
    {
    }
} 

DTE commands

Now that you have DTE in your hands, you can do a whole lot of stuff, for example, execute a command:

 dte.ExecuteCommand("File.OpenFile", "");

This one will execute the File.OpenFile command to display the open file dialog. There are plenty more Visual Studio commands that are really useful if you want to automate Visual Studio. You can look up a VS command from the Command Window: View –> Other Windows –> Command Window. Just start typing there and it will offer a completion list:

image

Also, you can use the Customize dialog (right-click on any VS menu) to get an idea of what commands are available:

image

Finally, you can see what command corresponds to an action if you start recording a macro, then just do an action manually, and then view the source code for that macro. As the macro is being recorded, VS registers all DTE command calls and writes them down in VBA source code. For example, ever wondered what command corresponds to the Rename refactoring? Record it and view the macro source, you’ll find out that there is a Refactor.Rename command.

Other DTE API

Apart from DTE.ExecuteCommand, there are a lot of other APIs to control the editor, ActiveDocument, ActiveWindow, Application, Debugger, Documents, ItemOperations, Solution, SourceControl, etc.

However this deserves a separate post by itself. Who knows, if there is popular demand on how to automate Visual Studio, I might start a series of blog posts about that. However, for now, I’ll just link to MSDN articles on DTE: