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.
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.
v
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.
- private NotePad(Package package) {
- if (package == null) {
- throw new ArgumentNullException("package");
- }
- this.package = package;
- OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
- if (commandService != null) {
- var menuCommandID = new CommandID(CommandSet, CommandId);
- var menuItem = new MenuCommand(this.OpenNotePad, menuCommandID);
- commandService.AddCommand(menuItem);
- }
- }
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) : e[f];
- }
Create a method called OpenNotePad and add the code given below.
- private void OpenNotePad(object sender, EventArgs e) {
- Process proc = new Process();
- proc.StartInfo.FileName = "Notepad.exe";
- proc.Start();
- }
Go to NotePadPackage.vsct file and update the ButtonText to Invoke NotePad
Create a method called OpenNotePad and add the code given below.
- <Button guid="guidNotePadPackageCmdSet" id="NotePadId" priority="0x0100" type="Button">
-
- <Parent guid="guidNotePadPackageCmdSet" id="MyMenuGroup" />
-
- <Icon guid="guidImages" id="bmpPic1" />
-
- <Strings>
-
- <ButtonText>Invoke NotePad</ButtonText>
-
- </Strings>
-
- </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.
- Process proc = new Process();
- proc.StartInfo.WorkingDirectory = @"Directory name";
- proc.StartInfo.FileName = "Application.exe";
- proc.Start();