FAQ: 1.2 How do I get a current selected project item? RRS feed


  • You can use the SVsShellMonitorSelection service to get the current selected IVsHierarchy, and then get the project item through its method GetProjectItem(), below code is the reply from Feng Chen in that forum thread.


    Code Sample:

    IntPtr hierarchyPtr, selectionContainerPtr;

    Object prjItemObject  = null;

    IVsMultiItemSelect mis;

    uint prjItemId;

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

    monitorSelection.GetCurrentSelection(out hierarchyPtr, out prjItemId, out mis, out selectionContainerPtr);

    IVsHierarchy selectedHierarchy = Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)) asIVsHierarchy;

    if (selectedHierarchy != null)


    ErrorHandler.ThrowOnFailure(selectedHierarchy.GetProperty(prjItemId, (int)__VSHPROPID.VSHPROPID_ExtObject, out prjItemObject));


    ProjectItem selectedPrjItem = prjItemObject asProjectItem;


    Related Thread:

    • Marked as answer by MSDN FAQ Wednesday, May 5, 2010 12:30 PM
    Wednesday, May 5, 2010 12:29 PM