![]() |
||||
|
This article describes how to delete commands and other permanent UI elements created by an add-in when it is uninstalled. More Information The commands created by an add-in should not be deleted when the add-in is unloaded, because it takes time to create them on each add-in startup and user customizations such as keyboard bindings would be lost. Instead, they should be removed when the add-in is uninstalled. The user interface (buttons and commandbars) created by an add-in can be temporary or permanent, as explained in the article HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in. If the user interface is temporary, the add-in removes it when it is unloaded. But if the user interface is permanent, it must be removed when the add-in is uninstalled. This article explains how to remove commands when the add-in is uninstalled. When a command is removed, the buttons that were created from that command using the Command.AddControl method are automatically removed. If the add-in creates permanent command bars, the same approach can be used to remove them on uninstallation. To remove the commands of the add-in, its uninstaller must perform a custom action. To learn how to create a custom action, check the documentation of your uninstaller. For the purpose of this article, we will use a VB script (.vbs file extension) for a custom action. The custom action must perform the following steps:
There is an unfortunate bug in Visual Studio .NET 2002/2003, though. Commands (and permanent command bars) are persisted on disk in the file:
where <version> is:
With the approach described so far, if the buttons created from commands of the add-in (created from commands using Command.AddControl) were added only to command bars created by the add-in (and not to built-in command bars of Visual Studio), it happens that when the commands are deleted the file is not marked as dirty and therefore although the uninstallation doesn't show any error, commands are not actually removed since that file is not updated. The workaround is to create at uninstallation time a button from the command on a built-in command bar of Visual Studio .NET such as the "Tools" menu. The following VB Script sample shows the final code of the custom action with the workaround:
Dim objDTE
Dim objCommand
Set objDTE = CreateObject("VisualStudio.DTE.7.1") ' Visual Studio .NET 2003
Set objCommand = objDTE.Commands.Item("MyAddIn.Connect.MyCommand")
' PATCH: add the command to the Tools menu
objCommand.AddControl(objDTE.CommandBars.Item("Tools"))
objCommand.Delete
objDTE.Quit;
Visual Studio 2005 and higher introduces a new handy /ResetAddin command-line flag for the devenv.exe application that can be used to remove the commands of an add-in and its associated permanent buttons. It doesn't remove the permanent commandbars created calling EnvDTE.Commands.AddCommandBar, though, because that method doesn't receive a parameter with the add-in instance, so Visual Studio doesn't know whether the owner of a permanent commandbar is an add-in to remove the commandbar when resetting its owner add-in. The usage is the following: devenv.exe /ResetAddIn Namespace.ClassName /Command File.Exit where Namespace.ClassName is the full name of the Connect class. Since the IDE will remain open, /Command File.Exit is used to close it. Since devenv.exe is not available from the normal MS-DOS command prompt, you will need to specify its full path, which can be retrieved from the Windows registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<version> registry key, "InstallDir" value. Where <version> is:
Finally, this command has no effect if the add-in is loaded when the IDE is loaded, so the uninstaller needs to unregister the add-in first, and then execute the command to remove the add-in commands. While the /SafeMode command-line flag could be used in Visual Studio 2008 or higher to prevent the loading of the add-in, it doesn't work in Visual Studio 2005 (see the article BUG: The /SafeMode command-line switch of Visual Studio 2005 doesn't prevent the loading of add-ins), so you must use the proper order in the steps of the uninstaller. The "HOWTO: Create a setup for a Visual Studio add-in" articles below show how to remove commands during the uninstallation of an add-in. Related articles
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||