Visual Studio Extensibility - Creating Your First Visual Studio VSIX Package - Day One

Introduction

Visual Studio Extensibility features are not new in .NET. It’s just that they are not very commonly used which, to me, is a surprise because Visual Studio Extensibility features are in themselves so powerful that it gives a new definition to customization. Customization of your IDE, customization of the desired features that every developer would love to have, and even customization on the IDE that could eventually result in a whole new product altogether i.e. a custom Visual Studio with one’s own extensions and features.


When we talk about extensibility, this is nothing but a literal term that we are talking about. Extensibility means adding some more features or customizing the existing implementation of any product to fulfill your needs.

In this "three article" series of Visual Studio Extensibility, we’ll learn how to create a new Visual Studio package, deploy that on staging server and GIT via continuous integration setup ,and at the end create a Visual Studio isolated Shell application with that embedded package. Although this is very rare topic and you could not find enough study material on this topic over the web. This explains how to work with it- step by step. MSDN contains good content but very generic, and to the point. I’ll, in my article, try to explain each and every small part step by step, so that one can learn while coding.

VSIX Packages

VSIX Visual Studio packages give us flexibility to customize Visual Studio as per our need and requirement. As a developer, one always wants that the IDE on which he is working should have certain features apart from the in-built one. You can read more about theoretical aspects and understanding the details of VSIX package here. Following is the small definition from the same MSDN link.

A VSIX package is a .vsix file that contains one or more Visual Studio extensions, together with the metadata Visual Studio uses to classify and install the extensions. That metadata is contained in the VSIX manifest and the [Content_Types].xml file. A VSIX package may also contain one or more Extension.vsixlangpack files to provide localized setup text, and may contain additional VSIX packages to install dependencies.

The VSIX package format follows the Open Packaging Conventions (OPC) standard. The package contains binaries and supporting files, together with a [Content_Types].xml file and a .vsix manifest file. One VSIX package may contain the output of multiple projects, or even multiple packages that have their own manifests. ”

The power of Visual Studio extensibility gives us that opportunity to create our own extensions and packages that we can build on top of existing Visual Studio and even distribute/sell those over Visual Studio market place https://marketplace.visualstudio.com/. For example, I could not find an option in Visual Studio to compare two files. So, I created my own Visual Studio extension to compare two files within Visual Studio. The extension could be downloaded from https://marketplace.visualstudio.com/items?itemName=vs-publisher-457497.FileComparer.

In the similar way, in this article, I will explain how we can create an extension in Visual Studio to open the selected file in Windows Explorer. You must have seen that we already have a feature to open the selected project/folder in Windows Explorer directly from Visual Studio, but won’t it be cool to get the feature that on right-click of file opens the selected file in Windows Explorer as well. So basically, we create the extensions for ourselves, or we can create an extension for our team members or as per project’s requirement and need, or even for fun and to explore the technology as well.
Roadmap

Let’ get more segregated and define a roadmap to achieve a proper working customized Isolated Shell application from Visual Studio. The series will be divided into three articles as mentioned below. We’ll focus more on practical implementations and hands-on rather than going much into theory.

  1. Visual Studio Extensibility (Day 1): Creating your first Visual Studio VSIX package
  2. Visual Studio Extensibility (Day 2): Deploying the VSIX package on staging server and GIT via Continuous Integration
  3. Visual Studio Extensibility (Day 3): Embedding VSIX package in Visual Studio Isolated Shell
Prerequisites

There are certain prerequisites that we need to take care of, while working on extensibility projects. If you have Visual Studio 2015 installed, go to Control panel >> Programs and features and search for Visual Studio 2015. Then, right click on it to select “change” option.


Here, we need to enable Visual Studio extensibility feature to work on this project type. On the next screen, click on “Modify”. A list of all selected/unselected features would be available now and all we need to do is to select "Visual Studio Extensibility Tools Update 3" in the Features-> Common Tools, as shown in following image.


Now, press Update button and let Visual Studio update to extensibility features after which we are good to go. Before we actually start, I need the readers to download and install Extensibility Tools written by Mads Kristensen from here.


This series is highly inspired with Mads Kristensen’s speech at Build 2016 and his work on Visual Studio extensibility.

Create VSIX Package

Now, we can create our own VSIX package inside Visual Studio. We’ll go step by step, therefore capturing every minor step and taking that into account. As I mentioned earlier, we’ll try to create an extension that allows us to open the selected Visual Studio file in Windows Explorer, something like shown in below image.


Step 1 Create a VSIX project

Let’s start from very basic. Open your Visual Studio. I am using Visual Studio 2015 Enterprise edition and would recommend you to use at least Visual Studio 2015 for this article


Create a new project like we create in every other project in Visual Studio. Select File->New->Project.


Now, in the Templates, navigate to Extensibility and select VSIX project. Note that these templates are shown here because we modified Visual Studio configuration to use Visual Studio Extensibility. Select VSIX project and give it a name. For example, I gave it a name “LoctateFolder”.


As soon as the new project is created, a “Getting Started” page is displayed with a lot of information and updates on Visual Studio extensibility. These are links to MSDN and useful resources that you can explore to learn more and almost everything about extensibility.

We got our project with a default structure to start with, which includes an HTML file, a CSS file, and a VSIXmanifest file. Manifest file, as the name suggests, keeps all the information related to the VSIX project and this file actually can be called a manifest to the extension created in the project.
 



We can clearly see that the “Getting Started” page that we see here comes from this index.html file which uses stylesheet.css. So, in our project, we really don’t need these files and we can remove these files.


And now, we are left only with the manifest file. So technically speaking, our step one has been accomplished, and we have created a VSIX project.

Step 2 Configure Manifest file

When we open the manifest file, we see certain kind of related information for the type of project that we added. We can modify this manifest file as per our choice for our extension. For e.g., in the ProductID, we can remove the text that is prefixed to the GUID and only keeps the GUID. Note that GUID is necessary as all the linking of items is done via GUID in VSIX projects. We’ll see this in more details later.


Similarly, add a meaningful description in the "Description box" like “Helps to locate files and folder in windows explorer”. This description is necessary as it tells what your extension is for.

And if you look at the code of the manifest file by selecting the file, right click and view code or just press F7 on the designer opened to view code. You’ll see an XML file that is created at the background and all this information is saved in a well-defined XML format.

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">  
  3.   <Metadata>  
  4.     <Identity Id="106f5189-471d-40ab-9de2-687c0a3d98e4" Version="1.0" Language="en-US" Publisher="Akhil Mittal" />  
  5.     <DisplayName>LocateFolder</DisplayName>  
  6.     <Description xml:space="preserve">Helps to locate files and folder in windows explorer.Helps </Description>  
  7.     <Tags>file locator, folder locator, open file in explorer</Tags>ption>  
  8.   </Metadata>  
  9.   <Installation>  
  10.     <InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0]" />  
  11.   </Installation>  
  12.   <Dependencies>  
  13.     <Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />  
  14.   </Dependencies>  
  15. </PackageManifest>   

Step 3 Add Custom Command

We have successfully added a new project and configured its manifest file but the real job is still pending -  writing an extension to locate the file. For that, we need to add a new item to our project, so just right click on the project and select to add a new item from the items template.


As soon as you open the item templates, you’ll see an option to add a new custom command under Visual C# items - > Extensibility. The custom commands act as a button in VSIX Extensions. These buttons help us to bind an action to its click event, so we can add our desired functionality to this button/command. Name the custom command you added. For e.g. - I gave it a name “LocateFolderCommand” and then press "Add" as shown in the below mentioned image.


Once the command is added, we can see a lot of changes happening to our existing project. Like adding of some required nugget packages, a Resources folder with an icon and an image, a .vsct file, a .resx file, and a command along with CommandPackage.cs file.


Each of the files has its own significance here. We’ll cover all these details. When we open the LocateFolderCommandPackage.vsct file, we again see an XML file.


And when you remove all the comments to make it more readable, you’ll get a file something like shown below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  3.   
  4.   <Extern href="stdidcmd.h"/>  
  5.   <Extern href="vsshlids.h"/>  
  6.   <Commands package="guidLocateFolderCommandPackage">  
  7.     <Groups>  
  8.       <Group guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">  
  9.         <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>  
  10.       </Group>  
  11.     </Groups>  
  12.     <Buttons>  
  13.       <Button guid="guidLocateFolderCommandPackageCmdSet" id="LocateFolderCommandId" priority="0x0100" type="Button">  
  14.         <Parent guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" />  
  15.         <Icon guid="guidImages" id="bmpPic1" />  
  16.         <Strings>  
  17.           <ButtonText>Invoke LocateFolderCommand</ButtonText>  
  18.         </Strings>  
  19.       </Button>  
  20.     </Buttons>  
  21.     <Bitmaps>  
  22.       <Bitmap guid="guidImages" href="Resources\LocateFolderCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>  
  23.     </Bitmaps>  
  24.   </Commands>  
  25.   <Symbols>  
  26.     <GuidSymbol name="guidLocateFolderCommandPackage" value="{a7836cc5-740b-4d5a-8a94-dc9bbc4f7db1}" />  
  27.     <GuidSymbol name="guidLocateFolderCommandPackageCmdSet" value="{031046af-15f9-44ab-9b2a-3f6cad1a89e3}">  
  28.       <IDSymbol name="MyMenuGroup" value="0x1020" />  
  29.       <IDSymbol name="LocateFolderCommandId" value="0x0100" />  
  30.     </GuidSymbol>  
  31.     <GuidSymbol name="guidImages" value="{8ac8d2e1-5ef5-4ad7-8aa6-84da2268566a}" >  
  32.       <IDSymbol name="bmpPic1" value="1" />  
  33.       <IDSymbol name="bmpPic2" value="2" />  
  34.       <IDSymbol name="bmpPicSearch" value="3" />  
  35.       <IDSymbol name="bmpPicX" value="4" />  
  36.       <IDSymbol name="bmpPicArrows" value="5" />  
  37.       <IDSymbol name="bmpPicStrikethrough" value="6" />  
  38.     </GuidSymbol>  
  39.   </Symbols>  
  40. </CommandTable>   

So, primarily the file contains groups, buttons (that are commands lying in that group), button text, and some IDSymbol, and image options.

When we talk about “Groups”, it is grouping of commands that are shown in Visual Studio. Like in the below image, when in Visual Studio, you click on Debug, you see various commands like Windows, Graphics, Start Debugging… etc.

Some are separated by horizontal lines as well. These separated horizontal lines are groups. So group is something that holds commands and acts as a logical separation between commands. In VSIX project, we can create a new custom command and also define the groups to which it will associate. We can create new groups as well or extend existing groups like shown in the .vsct XML file.

Step 4 Configure Custom Command

So, first, open the vsct file and let us decide where our command will be placed. We basically want our command to be visible when we right click on any file in Solution Explorer. For that, in the .vsct file, you can specify the parent of your command, since it is an item node, we can choose IDM_VS_CTXT_ITEMNODE.


You can check all the available locations at the following link. Similarly, we can also create menus, sub menus, and sub items, but for now, we’ll stick to our objective and place our command to item node.

Similarly, we can also define the position at which our command will be shown. Set the priority in the group, by default it is shown as 6th position as shown in the below image but you can always change it. For e.g., I changed the priority to 0X0200 so as to see my command at top level second position.


You can also change the default button text to “Open in File Explorer” and finally, after all the modifications, our XML looks as shown below. 

  1. “<?xml version="1.0" encoding="utf-8"?>  
  2. <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  3.   
  4.   <Extern href="stdidcmd.h"/>  
  5.   <Extern href="vsshlids.h"/>  
  6.   <Commands package="guidLocateFolderCommandPackage">  
  7.     <Groups>  
  8.       <Group guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" priority="0x0200">  
  9.         <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>  
  10.       </Group>  
  11.     </Groups>  
  12.     <Buttons>  
  13.       <Button guid="guidLocateFolderCommandPackageCmdSet" id="LocateFolderCommandId" priority="0x0100" type="Button">  
  14.         <Parent guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" />  
  15.         <Icon guid="guidImages" id="bmpPic1" />  
  16.         <Strings>  
  17.           <ButtonText>Open in File Explorer</ButtonText>  
  18.         </Strings>   
  19.       </Button>  
  20.     </Buttons>  
  21.     <Bitmaps>  
  22.       <Bitmap guid="guidImages" href="Resources\LocateFolderCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>  
  23.     </Bitmaps>  
  24.   </Commands>  
  25.   <Symbols>  
  26.     <GuidSymbol name="guidLocateFolderCommandPackage" value="{a7836cc5-740b-4d5a-8a94-dc9bbc4f7db1}" />  
  27.     <GuidSymbol name="guidLocateFolderCommandPackageCmdSet" value="{031046af-15f9-44ab-9b2a-3f6cad1a89e3}">  
  28.       <IDSymbol name="MyMenuGroup" value="0x1020" />  
  29.       <IDSymbol name="LocateFolderCommandId" value="0x0100" />  
  30.     </GuidSymbol>  
  31.     <GuidSymbol name="guidImages" value="{8ac8d2e1-5ef5-4ad7-8aa6-84da2268566a}" >  
  32.       <IDSymbol name="bmpPic1" value="1" />  
  33.       <IDSymbol name="bmpPic2" value="2" />  
  34.       <IDSymbol name="bmpPicSearch" value="3" />  
  35.       <IDSymbol name="bmpPicX" value="4" />  
  36.       <IDSymbol name="bmpPicArrows" value="5" />  
  37.       <IDSymbol name="bmpPicStrikethrough" value="6" />  
  38.     </GuidSymbol>  
  39.   </Symbols>  
  40. </CommandTable>   

When we open the LocateFolderCommand.cs, that’s the actual place where we need to put our logic. In VS extensibility project/command, everything is handled and connected via GUIDs. Here, we see in the below image that a command set is created with a new GUID.


Now, when you scroll down, you see in the private constructor, we retrieve the command service that is fetched from the current service provider. This service is responsible for adding the command, provided that the command has a valid menuCommandId with defined commandSet and commandId.


We also see that there is a call back method bound to the command. This is the same call back method that's called when the command is invoked, and that is the best place to put our logic. By default, this call back method comes with a default implementation of showing a message box that proves the command is actually invoked.


Let’s keep the default implementation for now and try to test the application. We can add business logic to open the file in Windows Explorer.

Step 5 Test custom command with default implementation

One may wonder how to test the default implementation. I would say, just compile and run the application. As soon as the application is run via F5, a new window will be launched that is similar to Visual Studio, as shown below.


Note that we are creating an extension for Visual Studio, so ideally it should be tested in Visual Studio itself, on how it should look and how it should work. A new Visual Studio instance is launched to test the command. Note that this instance of Visual Studio is called "Experimental Instance". As the name suggests, this is for testing our implementation, basically checking how the things will work and look like.

In the launched experimental instance, add a new project like we add in normal Visual Studio. Note that all the features in this experimental instance can be configured and switched to "ON" and "OFF" on need basis. We can cover the details in my third article i.e. when we discuss about Visual Studio Isolated Shell.


To be simple, choose a new console application and name it as per your choice. I named it ‘Sample”.


When the project is added to Solution Explorer, we see a common project structure. Remember, our functionality was to add a command to selected file in Visual Studio Solution Explorer. Now, we can test our implementation, just right click on any file and you can see the “Open in File Explorer” command in a new group in the context menu as shown in following image.

The text comes from the text that we defined for our command in VSCT file.
 

Before you click on the command, place a breakpoint on MenuItemCallback method in the command file. So, when the command is clicked, you can see the menuItemCallback method is invoked.


Since this method contains the code to show a message box, just press F5 and you see a message box with a defined title, as shown in the following image.

This proves that our command works, and we just need to put right logic here. We can certainly take a break and celebrate at this point.


Step 6 Add actual implementation

So now, this is the time to add our actual implementation. We already know the place, just need to code. For actual implementation, I have added a new folder to the project and named it Utilities and added a class to that folder and named it LocateFile.cs with the following implementation.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.CompilerServices;  
  6. using System.Runtime.InteropServices;  
  7. using System.Runtime.InteropServices.ComTypes;  
  8.   
  9. namespace LocateFolder.Utilities   
  10. {  
  11.     internal static class LocateFile  
  12.     {  
  13.         private static Guid IID_IShellFolder = typeof(IShellFolder).GUID;  
  14.         private static int pointerSize = Marshal.SizeOf(typeof(IntPtr));  
  15.   
  16.         public static void FileOrFolder(string path, bool edit = false)  
  17.         {  
  18.             if (path == null)  
  19.             {  
  20.                 throw new ArgumentNullException("path");  
  21.             }  
  22.             IntPtr pidlFolder = PathToAbsolutePIDL(path);  
  23.             try  
  24.             {  
  25.                 SHOpenFolderAndSelectItems(pidlFolder, null, edit);  
  26.             }  
  27.             finally  
  28.             {  
  29.                 NativeMethods.ILFree(pidlFolder);  
  30.             }  
  31.         }  
  32.   
  33.         public static void FilesOrFolders(IEnumerable<FileSystemInfo> paths)  
  34.         {  
  35.             if (paths == null)  
  36.             {  
  37.                 throw new ArgumentNullException("paths");  
  38.             }  
  39.             if (paths.Count<FileSystemInfo>() != 0)  
  40.             {  
  41.                 foreach (  
  42.                     IGrouping<string, FileSystemInfo> grouping in  
  43.                     from p in paths group p by Path.GetDirectoryName(p.FullName))  
  44.                 {  
  45.                     FilesOrFolders(Path.GetDirectoryName(grouping.First<FileSystemInfo>().FullName),  
  46.                         (from fsi in grouping select fsi.Name).ToList<string>());  
  47.                 }  
  48.             }  
  49.         }  
  50.   
  51.         public static void FilesOrFolders(IEnumerable<string> paths)  
  52.         {  
  53.             FilesOrFolders(PathToFileSystemInfo(paths));  
  54.         }  
  55.   
  56.         public static void FilesOrFolders(params string[] paths)  
  57.         {  
  58.             FilesOrFolders((IEnumerable<string>)paths);  
  59.         }  
  60.   
  61.         public static void FilesOrFolders(string parentDirectory, ICollection<string> filenames)  
  62.         {  
  63.             if (filenames == null)  
  64.             {  
  65.                 throw new ArgumentNullException("filenames");  
  66.             }  
  67.             if (filenames.Count != 0)  
  68.             {  
  69.                 IntPtr pidl = PathToAbsolutePIDL(parentDirectory);  
  70.                 try  
  71.                 {  
  72.                     IShellFolder parentFolder = PIDLToShellFolder(pidl);  
  73.                     List<IntPtr> list = new List<IntPtr>(filenames.Count);  
  74.                     foreach (string str in filenames)  
  75.                     {  
  76.                         list.Add(GetShellFolderChildrenRelativePIDL(parentFolder, str));  
  77.                     }  
  78.                     try  
  79.                     {  
  80.                         SHOpenFolderAndSelectItems(pidl, list.ToArray(), false);  
  81.                     }  
  82.                     finally  
  83.                     {  
  84.                         using (List<IntPtr>.Enumerator enumerator2 = list.GetEnumerator())  
  85.                         {  
  86.                             while (enumerator2.MoveNext())  
  87.                             {  
  88.                                 NativeMethods.ILFree(enumerator2.Current);  
  89.                             }  
  90.                         }  
  91.                     }  
  92.                 }  
  93.                 finally  
  94.                 {  
  95.                     NativeMethods.ILFree(pidl);  
  96.                 }  
  97.             }  
  98.         }  
  99.   
  100.         private static IntPtr GetShellFolderChildrenRelativePIDL(IShellFolder parentFolder, string displayName)  
  101.         {  
  102.             uint num;  
  103.             IntPtr ptr;  
  104.             NativeMethods.CreateBindCtx();  
  105.             parentFolder.ParseDisplayName(IntPtr.Zero, null, displayName, out num, out ptr, 0);  
  106.             return ptr;  
  107.         }  
  108.   
  109.         private static IntPtr PathToAbsolutePIDL(string path) =>  
  110.             GetShellFolderChildrenRelativePIDL(NativeMethods.SHGetDesktopFolder(), path);  
  111.   
  112.         private static IEnumerable<FileSystemInfo> PathToFileSystemInfo(IEnumerable<string> paths)  
  113.         {  
  114.             foreach (string iteratorVariable0 in paths)  
  115.             {  
  116.                 string path = iteratorVariable0;  
  117.                 if (path.EndsWith(Path.DirectorySeparatorChar.ToString()) ||  
  118.                     path.EndsWith(Path.AltDirectorySeparatorChar.ToString()))  
  119.                 {  
  120.                     path = path.Remove(path.Length - 1);  
  121.                 }  
  122.                 if (Directory.Exists(path))  
  123.                 {  
  124.                     yield return new DirectoryInfo(path);  
  125.                 }  
  126.                 else  
  127.                 {  
  128.                     if (!File.Exists(path))  
  129.                     {  
  130.                         throw new FileNotFoundException("The specified file or folder doesn't exists : " + path, path);  
  131.                     }  
  132.                     yield return new FileInfo(path);  
  133.                 }  
  134.             }  
  135.         }  
  136.   
  137.         private static IShellFolder PIDLToShellFolder(IntPtr pidl) =>  
  138.             PIDLToShellFolder(NativeMethods.SHGetDesktopFolder(), pidl);  
  139.   
  140.         private static IShellFolder PIDLToShellFolder(IShellFolder parent, IntPtr pidl)  
  141.         {  
  142.             IShellFolder folder;  
  143.             Marshal.ThrowExceptionForHR(parent.BindToObject(pidl, null, ref IID_IShellFolder, out folder));  
  144.             return folder;  
  145.         }  
  146.   
  147.         private static void SHOpenFolderAndSelectItems(IntPtr pidlFolder, IntPtr[] apidl, bool edit)  
  148.         {  
  149.             NativeMethods.SHOpenFolderAndSelectItems(pidlFolder, apidl, edit ? 1 : 0);  
  150.         }  
  151.   
  152.   
  153.         [ComImport, Guid("000214F2-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
  154.         internal interface IEnumIDList  
  155.         {  
  156.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  157.             int Next(uint celt, IntPtr rgelt, out uint pceltFetched);  
  158.   
  159.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  160.             int Skip([In] uint celt);  
  161.   
  162.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  163.             int Reset();  
  164.   
  165.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  166.             int Clone([MarshalAs(UnmanagedType.Interface)] out IEnumIDList ppenum);  
  167.         }  
  168.   
  169.         [ComImport, Guid("000214E6-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown),  
  170.          ComConversionLoss]  
  171.         internal interface IShellFolder  
  172.         {  
  173.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  174.             void ParseDisplayName(IntPtr hwnd, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,  
  175.                 [In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName, out uint pchEaten, out IntPtr ppidl,  
  176.                 [In, Out] ref uint pdwAttributes);  
  177.   
  178.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  179.             int EnumObjects([In] IntPtr hwnd, [In] SHCONT grfFlags,  
  180.                 [MarshalAs(UnmanagedType.Interface)] out IEnumIDList ppenumIDList);  
  181.   
  182.             [PreserveSig, MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  183.             int BindToObject([In] IntPtr pidl, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In] ref Guid riid,  
  184.                 [MarshalAs(UnmanagedType.Interface)] out IShellFolder ppv);  
  185.   
  186.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  187.             void BindToStorage([In] ref IntPtr pidl, [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,  
  188.                 [In] ref Guid riid,  
  189.                 out IntPtr ppv);  
  190.   
  191.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  192.             void CompareIDs([In] IntPtr lParam, [In] ref IntPtr pidl1, [In] ref IntPtr pidl2);  
  193.   
  194.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  195.             void CreateViewObject([In] IntPtr hwndOwner, [In] ref Guid riid, out IntPtr ppv);  
  196.   
  197.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  198.             void GetAttributesOf([In] uint cidl, [In] IntPtr apidl, [In, Out] ref uint rgfInOut);  
  199.   
  200.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  201.             void GetUIObjectOf([In] IntPtr hwndOwner, [In] uint cidl, [In] IntPtr apidl, [In] ref Guid riid,  
  202.                 [In, Out] ref uint rgfReserved, out IntPtr ppv);  
  203.   
  204.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  205.             void GetDisplayNameOf([In] ref IntPtr pidl, [In] uint uFlags, out IntPtr pName);  
  206.   
  207.             [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]  
  208.             void SetNameOf([In] IntPtr hwnd, [In] ref IntPtr pidl, [In, MarshalAs(UnmanagedType.LPWStr)] string pszName,  
  209.                 [In] uint uFlags, [Out] IntPtr ppidlOut);  
  210.         }  
  211.   
  212.         private class NativeMethods  
  213.         {  
  214.             private static readonly int pointerSize = Marshal.SizeOf(typeof(IntPtr));  
  215.   
  216.             public static IBindCtx CreateBindCtx()  
  217.             {  
  218.                 IBindCtx ctx;  
  219.                 Marshal.ThrowExceptionForHR(CreateBindCtx_(0, out ctx));  
  220.                 return ctx;  
  221.             }  
  222.   
  223.             [DllImport("ole32.dll", EntryPoint = "CreateBindCtx")]  
  224.             public static extern int CreateBindCtx_(int reserved, out IBindCtx ppbc);  
  225.   
  226.             [DllImport("shell32.dll", CharSet = CharSet.Unicode)]  
  227.             public static extern IntPtr ILCreateFromPath([In, MarshalAs(UnmanagedType.LPWStr)] string pszPath);  
  228.   
  229.             [DllImport("shell32.dll")]  
  230.             public static extern void ILFree([In] IntPtr pidl);  
  231.   
  232.             public static IShellFolder SHGetDesktopFolder()  
  233.             {  
  234.                 IShellFolder folder;  
  235.                 Marshal.ThrowExceptionForHR(SHGetDesktopFolder_(out folder));  
  236.                 return folder;  
  237.             }  
  238.   
  239.             [DllImport("shell32.dll", EntryPoint = "SHGetDesktopFolder", CharSet = CharSet.Unicode, SetLastError = true)  
  240.             ]  
  241.             private static extern int SHGetDesktopFolder_(  
  242.                 [MarshalAs(UnmanagedType.Interface)] out IShellFolder ppshf);  
  243.   
  244.             public static void SHOpenFolderAndSelectItems(IntPtr pidlFolder, IntPtr[] apidl, int dwFlags)  
  245.             {  
  246.                 uint cidl = (apidl != null) ? ((uint)apidl.Length) : 0;  
  247.                 Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems_(pidlFolder, cidl, apidl, dwFlags));  
  248.             }  
  249.   
  250.             [DllImport("shell32.dll", EntryPoint = "SHOpenFolderAndSelectItems")]  
  251.             private static extern int SHOpenFolderAndSelectItems_([In] IntPtr pidlFolder, uint cidl,  
  252.                 [In, Optional, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, int dwFlags);  
  253.         }  
  254.   
  255.         [Flags]  
  256.         internal enum SHCONT : ushort  
  257.         {  
  258.             SHCONTF_CHECKING_FOR_CHILDREN = 0x10,  
  259.             SHCONTF_ENABLE_ASYNC = 0x8000,  
  260.             SHCONTF_FASTITEMS = 0x2000,  
  261.             SHCONTF_FLATLIST = 0x4000,  
  262.             SHCONTF_FOLDERS = 0x20,  
  263.             SHCONTF_INCLUDEHIDDEN = 0x80,  
  264.             SHCONTF_INIT_ON_FIRST_NEXT = 0x100,  
  265.             SHCONTF_NAVIGATION_ENUM = 0x1000,  
  266.             SHCONTF_NETPRINTERSRCH = 0x200,  
  267.             SHCONTF_NONFOLDERS = 0x40,  
  268.             SHCONTF_SHAREABLE = 0x400,  
  269.             SHCONTF_STORAGE = 0x800  
  270.         }  
  271.     }  
  272. }  

This class contains the business logic , primarily methods that take file path as a parameter and work with shell to open this file in explorer. I’ll not go into details of this class, but focus more on how we can invoke this functionality.

Now, in the MenuItemCallBack method, put the following code to invoke the method of our utility class,

  1. private void MenuItemCallback(object sender, EventArgs e)  
  2.      {  
  3.          var selectedItems = ((UIHierarchy)((DTE2)this.ServiceProvider.GetService(typeof(DTE))).Windows.Item("{3AE79031-E1BC-11D0-8F78-00A0C9110057}").Object).SelectedItems as object[];  
  4.          if (selectedItems != null)  
  5.          {  
  6.              LocateFile.FilesOrFolders((IEnumerable<string>)(from t in selectedItems  
  7.                                                                          where (t as UIHierarchyItem)?.Object is ProjectItem  
  8.                                                                          select ((ProjectItem)((UIHierarchyItem)t).Object).FileNames[1]));  
  9.          }  
  10.      }  

This method now first fetches all the selected items using DTE object. With DTE objects, you can do all the transactions and manipulations in Visual Studio components. Read more about the power of DTE objects here.

After getting the selected items, we invoke the FilesOrFolders method of the utility class and pass the file path as a parameter. Job done. Now, again, launch the experimental instance and check the functionality.

Step 7 Test actual implementation

Launch experimental instance, add a new or existing project, and right click on any file followed by invoking the command.


As soon as you invoke the command, you’ll see that the folder is opened in Windows Explorer with that file selected, as shown below.


This functionality also works for the linked files in Visual Studio. Let’s check that. Add a new item in the project opened in experimental instance and add a file as a link, as shown in the following image.


You only need to select “Add as Link” while adding the file. This file would then be shown in Visual Studio with a different icon showing that this is a linked file. Now, select the actual Visual Studio file and the linked file in Visual Studio and invoke the command now.


When the command is invoked, you can see two folders opened with both the files selected at their own location.


Not only this, since we have created this extension in the Extensions and Updates. In this experimental instance, you can search for this extension and you’ll get it installed in your Visual Studio, as shown in the following image.


Now it’s time to celebrate again.



Step 7 Optimizing the package

Our job is nearly done but there are some more important things that we need to take care of. We need to make this package more appealing, add some image/icons to the extension, and optimize the project structure to make it more readable and understandable.

Remember, when we started this tutorial, I mentioned to download and install VS Extensibility Tools? VS Extensibility Tools provide some cool features that you can really leverage. For example, it allows you to export all the available images in Visual Studio. We can use these images to make our icon and default image for the extension. To start with, in Visual Studio where your code was written, go to “Tools >> Export Image Moniker…”


A window will open so as to search for the image you need to choose. Search for “Open”, and you’ll get the same image as shown in the context menu of project to open the project in Windows Explorer.


We’ll use this image only for our extension. Give it a size 16*16 and click "Export". Save that in your "Resources" folder of the project. Replace the already existing "LocateFolderCommand.png" file with this file and give this new exported file the same name. Since in the .vsct file, it was defined that the prior image sprint has to be used with first icon, so we always got to see 1X beside the custom command text, but we need a good looking meaningful image now. So, we exported this “open in explorer” image.


Now, go to .vsct file. In the Bitmaps, first delete all the image names in the list except bmpPic1 from the usedList and in the GuidSymbol, delete all IDsymbol except bmpPic1. We do not need to change the href link in Bitmap node because we replaced existing image with the newly exported image with the same name. We did this because we are now not using that old default image spirit but we are using now our newly exported image.


In that case, the LocateFolderCommandPackage.vsct file would look like this.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  3.   
  4.   <Extern href="stdidcmd.h"/>  
  5.   <Extern href="vsshlids.h"/>  
  6.   <Commands package="guidLocateFolderCommandPackage">  
  7.     <Groups>  
  8.       <Group guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" priority="0x0200">  
  9.         <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>  
  10.       </Group>  
  11.     </Groups>  
  12.     <Buttons>  
  13.       <Button guid="guidLocateFolderCommandPackageCmdSet" id="LocateFolderCommandId" priority="0x0100" type="Button">  
  14.         <Parent guid="guidLocateFolderCommandPackageCmdSet" id="MyMenuGroup" />  
  15.         <Icon guid="guidImages" id="bmpPic1" />  
  16.         <Strings>  
  17.           <ButtonText>Open in File Explorer</ButtonText>  
  18.         </Strings>   
  19.       </Button>  
  20.     </Buttons>  
  21.     <Bitmaps>  
  22.       <Bitmap guid="guidImages" href="Resources\LocateFolderCommand.png" usedList="bmpPic1"/>  
  23.     </Bitmaps>  
  24.   </Commands>  
  25.   <Symbols>  
  26.     <GuidSymbol name="guidLocateFolderCommandPackage" value="{a7836cc5-740b-4d5a-8a94-dc9bbc4f7db1}" />  
  27.     <GuidSymbol name="guidLocateFolderCommandPackageCmdSet" value="{031046af-15f9-44ab-9b2a-3f6cad1a89e3}">  
  28.       <IDSymbol name="MyMenuGroup" value="0x1020" />  
  29.       <IDSymbol name="LocateFolderCommandId" value="0x0100" />  
  30.     </GuidSymbol>  
  31.     <GuidSymbol name="guidImages" value="{8ac8d2e1-5ef5-4ad7-8aa6-84da2268566a}" >  
  32.       <IDSymbol name="bmpPic1" value="1" />  
  33.     </GuidSymbol>  
  34.   </Symbols>  
  35. </CommandTable>   

The next step is to set extension image and a preview image that would be shown for the extension in Visual Studio gallery and Visual Studio market place. These images will represent the extension everywhere.

So, follow the same routine of exporting image from Image Monikor. Note that you can also use your own custom images for all the image/icon related operations.

Open the image moniker like explained earlier and search for LocateAll, then export two images, one for icon (90 X 90)


and one for preview (175 X 175).


Export both the images with the name Icon.png and Preview.png respectively in the Resources folder. Then, in the Solution Explorer, include those two images in the project, as shown below.


Now, in the source.extension.vsixmanifest file, set the Icon and Preview images to the same exported images as shown in following image.


Step 8 Test final package

Again, it’s time to test the implementation with new Images and icons. So compile the project and press F5, experimental instance would launch. Add a new or existing project and right click on any project file to see your custom command.


So now we got the icon that was earlier selected from Image Moniker for this custom command. Since we have not touched the functionality, it should work fine as before.

Now go to extensions and updates and search for the installed extension “LocateFolder”. You’ll see a beautiful image before your extension, this is the same image with dimensions 90X90 and in the right side panel, you can see the enlarged 175X175 preview image.


Now we can certainly celebrate as the task is completely accomplished.



Conclusion

This detailed article focused on how a Visual Studio extension could be created. In the next article, I’ll explain how the project structure could be optimized to make it more readable and understandable and how to deploy the extension to Visual Studio Market Place via continuous integration and GIT. The basic idea would be to optimize the structure, push the code to GIT, push the extension to Visual Studio Gallery via continuous integration through AppVeyor, and push the extension to VS market place. I hope, this article helped you understand the Visual Studio extensibility. Feel free to share your feedback, ratings, and comments.

References
  • https://channel9.msdn.com/events/Build/2016/B886
  • https://blogs.msdn.microsoft.com/quanto/2009/05/26/what-is-a-vsix
  • https://msdn.microsoft.com/en-us/library/dd997148.aspx
Complete Source Code

https://github.com/akhilmittal/LocateFileInWindowsExplorer

Extension at marketplace

https://marketplace.visualstudio.com/items?itemName=vs-publisher-457497.LocateFolder

Read more

Up Next
    Ebook Download
    View all
    Learn
    View all