How To Create Visual Studio Extensions Using Visual Studio 2017

Introduction

This article is regarding creation of Visual Studio extensions, using Visual Studio 2017, so that we can start making custom extensions.

Let us start creating the extension with the menu command, which will launch one simple Application.

Prerequisite

There are anumber of extensions available for Visual Studio and now let us create our own extension, and make use of that extension. We need to have Visual Studio 2017 installed to create the extensions.

To create a custom extension, we need to install the Extensibility extension installed to Visual Studio 2017.

Once that the Extensibility extension is installed, we can start creating any new extension.

Creation of custom extension

To start creating the Extension, follow the steps given below.

Open Visual Studio 2017 and click File->New->Project.

New Project Window will be pop up.

Under Installed-> Templates-> Extensibility, select VSIX Project.

Name the project as the SampleProject and click OK button.

Visual Studio

Once the project is created, add New item to the project by clicking Add-> New Item.

In the pop-up Window, under Extensibility, select Custom command and name it as NotePad.cs.

vVisual Studio

Add the namespace, using System.Diagnostics.

New file will be added to the project and find the NotePad Constructor, update the constructor with the code given below.

  1. private NotePad(Package package) {  
  2.     if (package == null) {  
  3.         throw new ArgumentNullException("package");  
  4.     }  
  5.     this.package = package;  
  6.     OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;  
  7.     if (commandService != null) {  
  8.         var menuCommandID = new CommandID(CommandSet, CommandId);  
  9.         var menuItem = new MenuCommand(this.OpenNotePad, menuCommandID);  
  10.         commandService.AddCommand(menuItem);  
  11.     }  
  12. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  13. if ('this_is' == /an_example/) {  
  14.     of_beautifier();  
  15. else {  
  16.     var a = b ? (c % d) : e[f];  
  17. }   

Create a method called OpenNotePad and add the code given below.

  1. private void OpenNotePad(object sender, EventArgs e) {  
  2.     Process proc = new Process();  
  3.     proc.StartInfo.FileName = "Notepad.exe";  
  4.     proc.Start();  
  5. }   

Go to NotePadPackage.vsct file and update the ButtonText to Invoke NotePad

Create a method called OpenNotePad and add the code given below.

  1. <Button guid="guidNotePadPackageCmdSet" id="NotePadId" priority="0x0100" type="Button">  
  2.   
  3.    <Parent guid="guidNotePadPackageCmdSet" id="MyMenuGroup" />  
  4.   
  5.    <Icon guid="guidImages" id="bmpPic1" />  
  6.   
  7.    <Strings>  
  8.   
  9.    <ButtonText>Invoke NotePad</ButtonText>  
  10.   
  11.    </Strings>  
  12.   
  13. </Button>  

Once this is added, just build the solution and run it.

New instance of the Visual Studio 2017 will open in Experimental Instance mode.

Under Tools menu, we can find Invoke NotePad, click it to see that notepad is invoked.

If you want to add your custom Application to the tool menu, add the code given below in OpenNotePad method.

  1. Process proc = new Process();  
  2. proc.StartInfo.WorkingDirectory = @"Directory name";  
  3. proc.StartInfo.FileName = "Application.exe";  
  4. proc.Start();