1

NuGet package restore does not play well with the PostSharp package, this is of great annoyance to me and manually editing the csproj files is quickly becoming old.

I want to make a small Visual Studio AddIn that fixes the project for me by adding an extra button to the unloaded project context menu.

The problem I am facing sounds simple but haunts me: I somehow can't locate the CommandBar for the unloaded project context menu.

Thus, summarizing the question: What is the CommandBar name for the unloaded project context menu.

Many thanks!

Dennis Smit
  • 1,168
  • 8
  • 12
  • Since an unloaded project is not well accessible through the envDTE, someone wrote a good solution for this (opening the solution, and use that to use MSBuild to open the project): http://stackoverflow.com/questions/707107/library-for-parsing-visual-studio-solution-files – Dennis Smit Jan 04 '12 at 22:29

1 Answers1

1

Apparantly: "Stub Project". Found in the list at: CommandBar names

To sum up the OnConnection method to register this:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        if (connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object[] contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;
            string toolsMenuName = "Tools";

            //Place the command on the tools menu.
            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
            CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
            CommandBar vsBarProject = cmdBars["Stub Project"];
            CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

            //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //  just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                //Add a command to the Commands collection:
                Command command = commands.AddNamedCommand2(_addInInstance, "FixNuGetPostSharp", "Fix NuGet PostSharp", "Executes the command for VsextensionTest", true, 0, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                //Add a control for the command to the tools menu:
                if ((command != null) && (toolsPopup != null))
                {
                    command.AddControl(vsBarProject);
                }
            }
            catch (System.ArgumentException)
            {
                //If we are here, then the exception is probably because a command with that name
                //  already exists. If so there is no need to recreate the command and we can 
                //  safely ignore the exception.
            }
        }
    }
Community
  • 1
  • 1
Dennis Smit
  • 1,168
  • 8
  • 12