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)

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
    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:


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


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.


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:

Comments (7)

  1. Thank you for submitting this cool story – Trackback from DotNetShoutout

  2. Nelson T says:

    This is a great article. I think there is a large audience interested in this subject, and would benefit from you posting more blogs of this type in the near future.

    Personally I am interested in finding out how to expose new custom functionality as command alias for DTE.ExecuteCommand().

    For example, if I have developed a VS Package that provides connectivity to IBM Clearcase, how can I expose the available methods to DTE. My client will say want to connect to the Clearcase server using the following:


  3. Tushar says:

    if I want start exeperimental hive how do I do that

    for normal VS it is

           Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.9.0");

           DTE dte = Activator.CreateInstance(visualStudioType) as DTE;

           dte.MainWindow.Visible = true;

    basically for devenev.exe /RootSuffix exp

    what is it?

  4. Earlier I posted some code to start Visual Studio using C# 3.0: using System; using EnvDTE; class Program

    1. yosri says:

      i need help to lunch visual studio and open a solution and compile its project

  5. kiquenet says:

    Can I use

    DTE dte = Activator.CreateInstance(visualStudioType) as DTE;

    for open VS, ans solution and too disabled Addins when VS (DTE) is opened?


  6. sonal says:

    i need code for opening and closing an instance of visual studio

Skip to main content