Click here to Skip to main content
15,175,307 members
Articles / Programming Languages / C#
Posted 27 Mar 2011


24 bookmarked

How to Create a Visual Studio Add-in that Launches from the Project Window

Rate me:
Please Sign up or sign in to vote.
4.90/5 (3 votes)
27 Mar 2011CPOL3 min read
A step by step guide on how to create a Visual Studio add-in that launches from the Project window.


Within Visual Studio 2010, there are two main ways to install add-ons that allow you to make modifications to a Visual Studio project. The first way is to use Visual Studio templates. Templates are a great way to duplicate work that you would commonly need to do such as add license information to the top of every class. Another way is to use add-ins. Add-ins typically run from the Visual Studio Tools menu and allow you to run a program to do something. In our case, we were writing a component for our customers that would modify the Windows Azure service config file, and add-ins seemed to be the best way to accomplish this. The main issue with add-ins is that they run from the Tools menu so if you want to be able to make changes to a project of the user's choice, there is no way to do this unless you actually ask the user which project to modify. This would give a very bad user experience. What would be best is if a user could right click on a project to launch the add-in. Surprisingly, we could find very little information on the subject. Luckily, it is quite simple. This tutorial will walk you through the process.

Using the Code

To get started, open Visual Studio 2010 and create a new project and choose Other Project Types | Extensibility | Visual Studio Add-in. Leave the Name as the default name MyAddin1, and press OK.


As you walk through the wizard, you can leave the defaults (including choosing C#), except in “Choose Add-in Options”, you will want to ensure that you have selected the first two options:


After you finalize the wizard, it will create a blank Add-in project.

At this point, you have a working add-in. If you were to build the project and run it, you would see the add-in within the Tools menu.

Note: If you find the add-in is not loading, you can manually copy the two files MyAddin1.dll and MyAddin1.Addin to your C:\Users\<USERNAME>\Documents\Visual Studio 2010\Addins directory and reload Visual Studio.


This would be handy in many cases; however, we want to enable this functionality from the Project menu in Solution Explorer. To do this, we will need to modify the Connect.cs file. Open the Connect.cs file from the Solution Explorer. Locate the OnConnection method and replace it with the following code:

public void OnConnection(object application, ext_ConnectMode connectMode, 
                         object addInInst, ref Array custom)
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    if (connectMode == ext_ConnectMode.ext_cm_UISetup)
        object[] contextGUIDS = new object[] { };
        Commands2 commands = (Commands2)_applicationObject.Commands;
        Microsoft.VisualStudio.CommandBars.CommandBar standardToolBar = 
            Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", 
                              "My Addin...", "Executes the command for My Addin", 
                              true, 59, ref contextGUIDS, 
                              (int)vsCommandStatus.vsCommandStatusSupported + 
            if ((command != null) && (standardToolBar != null))
                CommandBarControl ctrl = 
                  (CommandBarControl)command.AddControl(standardToolBar, 1);
                ctrl.TooltipText = "Executes the command for MyAddin";
        catch (System.ArgumentException)

The real trick to this is the part of the code that includes ["Project"]. This tells Visual Studio to load the add-in from the Project menu. This is also how you would change the add-in to be loaded from the Solution menu within Solution Explorer.

That is all there is to it. Now if you build and run the add-in, it will allow you to launch the add-in from the Project menu. Give it a try by running the add-in. This will launch a new instance of Visual Studio. Create a new Visual Studio project (such as a Windows Form Application). Right click on the Project menu in the Solution Explorer and you should see your new add-in.


Points of Interest

If you would like to change the icon from the default happy face, locate this line and change the value from 59 to one of the other default icon numbers:

Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", 
                  "My Addin...", "Executes the command for My Addin", 
                  true, 59, ref contextGUIDS, 
                  (int)vsCommandStatus.vsCommandStatusSupported + 

You will also note that when you click on the add-in, the code that is called is the following. You can load your own add-in logic where it says: handled = true;.

public void Exec(string commandName, vsCommandExecOption executeOption, 
                 ref object varIn, ref object varOut, ref bool handled)
    handled = false;
    if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
        if(commandName == "MyAddin1.Connect.MyAddin1")
            handled = true;


  • 27th March, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Voclare
Product Manager
United States United States
John has been building cloud based services on Windows Azure since its inception. His specialty is in working with databases such as SQL Azure. John is currently working on Cotega a SQL Azure Monitoring service that provides database monitoring, alerts and reporting of SQL Azure and cloud hosted SQL Server databases.

Comments and Discussions

QuestionThank you Pin
amSherihan22-May-13 8:58
MemberamSherihan22-May-13 8:58 
GeneralAdd-Ins are kinda obsolete... Pin
Daniel Fisher (lennybacon)28-Mar-11 6:22
MemberDaniel Fisher (lennybacon)28-Mar-11 6:22 
GeneralRe: Add-Ins are kinda obsolete... Pin
John Voclare28-Mar-11 14:47
MemberJohn Voclare28-Mar-11 14:47 
GeneralA bit short Pin
Wendelius26-Mar-11 13:19
mveWendelius26-Mar-11 13:19 
GeneralRe: A bit short Pin
devnet24727-Mar-11 8:32
Memberdevnet24727-Mar-11 8:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.