Introduction to Visual Studio Add-ins


In this article, we will look into visual studio add-ins and benefits of using it. Visual Studio provides add-ins to extend the functionality to its IDE. By using add-ins, we can automate our repetitive tasks in VS IDE. Visual Studio provides two project templates for creating add-ins:

  • Visual Studio add-ins: This add-in works with only Visual Studio and its Macros.
  • Visual Studio Shared add-ins: This add-in works with both Visual Studio and Microsoft Office products like MS Word, MS Excel etc.

An add-in is a compiled component that runs inside Visual Studio IDE. VS provides a wizard to simplify the task of creating add-ins. Since, add-in is in compiled format, we can distribute those without exposing code and improving performance. Add-in wizard creates minimum settings for writing our own add-in.

Let's create a sample add-in. Open your VS 2008, go to New Project Other Project Types Extensibility Select Visual Studio Add-in Name as SampleAddIn as shown below:

It will show up a wizard to create the basic configuration/code to create the add-in. Select language as C# as shown below:

Then, click on Next and select Application Host as VS 2008 and VS 2008 Macros as shown below:

Then, click on Next and give Name & Description as shown below:

Then, click on Next and check the first checkbox for making our add-in gets displayed in Tools Menu of VS IDE and check second checkbox to load our add-in on start of host application (Visual Studio) a shown below:

Then, click on Next and check the first checkbox, if you want to provide any About dialog box to your add-in as shown below:

Then click on Finish, so we are done with basic settings. Now, we will make our add-in to log all the build events of a solution in a text file. Go to Exec method of Connect class and add below code to it:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

                   {

                             handled = false;

if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

                             {

                   if(commandName == "SampleAddIn.Connect.SampleAddIn")

                                      {

                                                handled = true;

_applicationObject.Events.BuildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(BuildEvents_OnBuildBegin);

 

_applicationObject.Events.BuildEvents.OnBuildDone += new _dispBuildEvents_OnBuildDoneEventHandler(BuildEvents_OnBuildDone);

                                                return;

                                      }

                             }

                   }

 

void BuildEvents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action)

        {

string logname = Path.GetFileNameWithoutExtension(_applicationObject.Solution.FullName) + ".txt"; StreamWriter writer = new StreamWriter("C:\\" + logname, true);

writer.WriteLine("Build Ended at" + DateTime.Now.ToString());

writer.WriteLine("---------------------------------");

writer.Flush();

writer.Close();

        }

 

void BuildEvents_OnBuildBegin(vsBuildScope Scope, vsBuildAction Action)

        {

string logname = Path.GetFileNameWithoutExtension(_applicationObject.Solution.FullName)+".txt";

            StreamWriter writer = new StreamWriter("C:\\" + logname, true);

 writer.WriteLine("---------------------------------");

 writer.WriteLine("Build Started for Solution at " + _applicationObject.Solution.FileName);

 writer.WriteLine("Having " + _applicationObject.Solution.Projects.Count.ToString()+" Projects");

 writer.WriteLine("Projects are :");

 for(int j=1;j<=_applicationObject.Solution.Projects.Count;j++)

         {

 for (int i = 1; i <=_applicationObject.Solution.Projects.Count; i++)

                {

writer.WriteLine(_applicationObject.Solution.Projects.Item(j).FullName);

                }

         }

writer.WriteLine("Build Started at" + DateTime.Now.ToString());

writer.Flush();

writer.Close();

        }

Whenever, we run our add-in by going to Tools  SampleAddIn. It will call Exec method, in that method call we are attaching handlers to BuildBegin and BuildDone events. So, whenever you build a solution, it will fire thes two events. In this event handlers, we are just saving build time and projects in it by looping through its Projects collection to a text file.

Now, run the add-in. It will automatically opens an instance of VS for testing our add-in. Create a sample project and goto Tools select SampleAddIn. Build the project.Go to C: drive and check there is a file named as sampleprojectname.txt having build start and end time along with path of each project in it.

In this way, we can extend our VS IDE. We can even add Windows Forms to our add-in for UI. In next article, we will see how to integrate win forms with our add-in.

I am ending the things here. I am attaching source code for reference. I hope this article will be helpful for all.


Similar Articles