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

Answers

  • 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:

    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/24f71d7f-bb7d-4df8-a00a-ff0ce6f4d48f
    • Marked as answer by MSDN FAQ Wednesday, May 5, 2010 12:30 PM
    Wednesday, May 5, 2010 12:29 PM