Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected project from Solution Explorer

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

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

I've managed to get the context menu to appear when right-clicking the Project nodes, but my next challenge is to get a reference to the Project object that's been clicked. Currently I am able to 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 do I get a similar reference to the project selected in the solution explorer?

like image 916
havardhu Avatar asked Jun 15 '12 11:06

havardhu


People also ask

How do I show selected files in Solution Explorer Visual Studio?

In Visual Studio's menu go to Tools > Options > Projects and Solutions. Then check "Track Active Item in Solution Explorer".

How do you run a specific project in a solution?

You can also edit the Startup Project in the Solution options (right click on the Solution name in the Solution View and choose Options, then select the Startup Project item in the list on the left, here you can also set multiple Startup Projects. To run a specific Project, right click on its name and choose Run Item.

How do I add a project in Solution Explorer?

In Solution Explorer, select the solution. On the File menu, point to Add, and click Existing Project. In the Add Existing Project dialog box, locate the project you want to add, select the project file, and then click Open. The project is added to the selected solution.

How do I open the project in Solution Explorer Visual Studio?

Open Solution Explorer If you don't see the Solution Explorer tool window, you can open it from the Visual Studio menu bar by using View > Solution Explorer, or by pressing Ctrl+Alt+L.


1 Answers

I figured it out. Might as well share the info.

By using the SVsShellMonitorSelection service I can get a reference to the selected hierarchy as a IVsHierarchy, which in turn allows me to get a reference to the selected object. This may then be cast to classes such as Project, ProjectItem, etc, based on what is selected in the Solution Explorer. 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;

Here's the source

like image 183
havardhu Avatar answered Sep 20 '22 17:09

havardhu