In this How Do I series, I will show developers how they can create a Visual Studio Extension with a Menu Command.
Let’s Start.
Creating a Menu Command
- Create a VSIX project named FirstMenuCommand. You can find the VSIX project template in the New Project dialog under Visual C# / Extensibility.
- When the project opens, add a custom command item template named FirstCommand. In the Solution Explorer, right-click the project node and select Add / New Item. In the Add New Item dialog, go to Visual C# / Extensibility and select Custom Command. In the Name field at the bottom of the window, change the command file name to FirstCommand.cs.
- Build the project and start debugging.
- In the experimental instance, open the Tools / Extensions and Updates window. You should see the FirstMenuCommand extension here. (If you open Extensions and Updates in your working instance of Visual Studio, you won’t see FirstMenuCommand).
Now go to the Tools menu in the experimental instance. You should see Invoke FirstCommand command. At this point it just brings up a message box that says “FirstCommandPackage Inside FirstMenuCommand.FirstCommand.MenuItemCallback()”. We’ll see how to actually start Notepad from this command in the next section.
- Stop debugging and go back to your working instance of Visual Studio. Open the FirstCommand.cs file and add the following using statement:
using System.Diagnostics;
- Find the private FirstCommand constructor. This is where the command is hooked up to the command service and the command handler is specified. Change the name of the command handler to StartNotepad, as follows:
C#
private FirstCommand(Package package) { if (package == null) { throw new ArgumentNullException(nameof(package)); } this.package = package; OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (commandService != null) { CommandID menuCommandID = new CommandID(CommandSet, CommandId); // Change to StartNotepad handler. MenuCommand menuItem = new MenuCommand(this.StartNotepad, menuCommandID); commandService.AddCommand(menuItem); } } - Remove the MenuItemCallback method and add a StartNotepad method which will just start Notepad:
private void StartNotepad(object sender, EventArgs e) { Process proc = new Process(); proc.StartInfo.FileName = "notepad.exe"; proc.Start(); } - Now try it out. When you start debugging the project and click Tools / Invoke FirstCommand, you should see an instance of Notepad come up.
You can use an instance of the Process class to run any executable, not just Notepad. Try it with calc.exe, for example.
If you are developing multiple extensions, or just exploring outcomes with different versions of your extension code, your experimental environment may stop working the way it should. In this case, you should run the reset script. It’s called Reset the Visual Studio 2015 Experimental Instance, and it ships as part of the Visual Studio SDK. This script removes all references to your extensions from the experimental environment, so you can start from scratch.
You can get to this script in one of two ways:
- From the desktop, find Reset the Visual Studio 2015 Experimental Instance.
- From the command line, run the following:
<VSSDK installation>\VisualStudioIntegration\Tools\Bin\CreateExpInstance.exe /Reset /VSInstance=14.0 /RootSuffix=Exp && PAUSE
Now that you have your tool extension running the way you want, it’s time to think about sharing it with your friends and colleagues. That’s easy, as long as they have Visual Studio 2015 installed. All you have to do is send them the .vsix file you built. (Be sure to build it in Release mode.)
You can find the .vsix file for this extension in the FirstMenuCommand bin directory. Specifically, assuming you have built the Release configuration, it will be in:
<code directory>\FirstMenuCommand\FirstMenuCommand\bin\Release\ FirstMenuCommand.vsix
To install the extension, your friend needs to close all open instances of Visual Studio, then double-click the .vsix file, which brings up the VSIX Installer. The files are copied to the %LocalAppData%\Microsoft\VisualStudio\14.0\Extensions directory.
When your friend brings up Visual Studio again, he’ll find the FirstMenuCommand extension in Tools / Extensions and Updates. He can go to Extensions and Updates to uninstall or disable the extension, too 🙂
