Search code examples
visual-studio-2015vsixvs-extensibility

Can I programatically add a new named command in visual studio extensibility?


Visual Studio 2015.

I used to use AddNamedCommand2 in an add-in. Now that add-ins are gone, I'm trying to learn making a vsix.

I'd like to be able to programatically add named commands.

Is this still possible? I used to use reflection to add a bunch of named commands to visual studio in an add-in. By this I mean commands I can run from the Command Window with a name I specify, like "Penguin.TestCommand".


Solution

  • Notice that the preferred way to create commands is the .vsct file, although you don't have complete control over the name (the prefix). To get the "Penguin." prefix your extension would have to create a top menu, which is overkill if your extension has only one command.

    Programatically, use IVsProfferCommands3.AddNamedCommand. There is a bug in versions of Visual Studio until VS 2013 (included) that, while the command is created, it is always disabled and you can't do anything to enable it. I reported this privately to Microsoft and they told me that the bug was fixed for VS 2015, although I have pending the verification and to write an article on my blog to document it.

    EDIT: See full sample "PackageCommandsCreatedByCode" that I have created for VS 2015: https://github.com/visualstudioextensibility/VSX-Samples