Selected Project from Solution Explorer

I am writing a customization package for Visual Studio 2010 (vsix).

What I need to do is add a context menu button to the project nodes in Solution Explorer.

I managed to display the context menu when I right-click on the project nodes, but the next task is to get a link to the project object that was clicked. Currently, I can get the project by going through the active document in the IDE using the code below.

DTE dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
Project project = dte.ActiveDocument.ProjectItem.ContainingProject;

So my question is: how to get a similar link to the project selected in the solution explorer?

+5
source share
1 answer

I get it. You can also share information.

SVsShellMonitorSelection, IVsHierarchy, , , . , Project, ProjectItem .., , . Handy!

IntPtr hierarchyPointer, selectionContainerPointer;
Object selectedObject  = null;
IVsMultiItemSelect multiItemSelect;
uint projectItemId;

IVsMonitorSelection monitorSelection = 
        (IVsMonitorSelection)Package.GetGlobalService(
        typeof(SVsShellMonitorSelection));

monitorSelection.GetCurrentSelection(out hierarchyPointer, 
                                     out projectItemId, 
                                     out multiItemSelect, 
                                     out selectionContainerPointer);

IVsHierarchy selectedHierarchy = Marshal.GetTypedObjectForIUnknown(
                                     hierarchyPointer, 
                                     typeof(IVsHierarchy)) as IVsHierarchy;

if (selectedHierarchy != null)
{
    ErrorHandler.ThrowOnFailure(selectedHierarchy.GetProperty(
                                      projectItemId,
                                      (int)__VSHPROPID.VSHPROPID_ExtObject, 
                                      out selectedObject));
}

Project selectedProject = selectedObject as Project;

+11

All Articles