private IEnumerable <string> GetSelectedFileFullPaths() { IVsMultiItemSelect multiItemSelect; uint itemId; IntPtr hierarchyPtr; IntPtr selectionContainerPtr; var currentSelectionResult = (GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection). GetCurrentSelection(out hierarchyPtr, out itemId, out multiItemSelect, out selectionContainerPtr); if (ErrorHandler.Failed(currentSelectionResult) || hierarchyPtr == IntPtr.Zero || itemId == VSConstants.VSITEMID_NIL || itemId == VSConstants.VSITEMID_ROOT) { return(Enumerable.Empty <string>()); } if (multiItemSelect == null) { return(new[] { GetSelectedFileFullPath(itemId, Marshal.GetObjectForIUnknown(hierarchyPtr) as IVsHierarchy) }); } uint selectedItemCount; int pfSingleHirearchy; multiItemSelect.GetSelectionInfo(out selectedItemCount, out pfSingleHirearchy); var selectedItems = new VSITEMSELECTION[selectedItemCount]; multiItemSelect.GetSelectedItems(0, selectedItemCount, selectedItems); return(selectedItems.Select(_ => GetSelectedFileFullPath(_.itemid, _.pHier))); }
public IEnumerable <string> GetFullFilePath(IServiceProvider serviceProvider) { var selectionMonitor = serviceProvider.GetService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection; if (selectionMonitor == null) { Debug.Fail("Failed to get SVsShellMonitorSelection service."); return(Enumerable.Empty <string>()); } int hr = selectionMonitor.GetCurrentSelection(out IntPtr hierarchyPtr, out uint itemid, out IVsMultiItemSelect multiSelect, out IntPtr containerPtr); if (hierarchyPtr == IntPtr.Zero) { return(Enumerable.Empty <string>()); } if (IntPtr.Zero != containerPtr) { Marshal.Release(containerPtr); containerPtr = IntPtr.Zero; } Debug.Assert(hr == VSConstants.S_OK, "GetCurrentSelection failed."); if (itemid == (uint)VSConstants.VSITEMID.Selection) { multiSelect.GetSelectionInfo(out uint count, out int somethingElse); if (count > 0) { VSITEMSELECTION[] selectedItems = new VSITEMSELECTION[count]; var sel = multiSelect.GetSelectedItems((uint)__VSGSIFLAGS.GSI_fOmitHierPtrs, count, selectedItems); //multiple selection return(selectedItems.Select(info => GetItemPath(hierarchyPtr, info.itemid))); } else { return(Enumerable.Empty <string>()); } } else { return(new string[] { GetItemPath(hierarchyPtr, itemid) }); } }
/// <summary> /// Gets the selected projects. /// </summary> /// <returns></returns> public static IEnumerable <IVsHierarchy> GetSelectedProjects() { List <IVsHierarchy> list = new List <IVsHierarchy>(); IntPtr hierarchyPointer = IntPtr.Zero; uint itemCount = 0; IntPtr ppSC = IntPtr.Zero; int singleItemPointer = 0; IVsMultiItemSelect multiSelect = null; try { IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsShellMonitorSelection)); ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection( out hierarchyPointer, out itemCount, out multiSelect, out ppSC)); if (hierarchyPointer != IntPtr.Zero) { IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(hierarchyPointer); list.Add(hierarchy); } else if (multiSelect != null) { ErrorHandler.ThrowOnFailure(multiSelect.GetSelectionInfo(out itemCount, out singleItemPointer)); VSITEMSELECTION[] items = new VSITEMSELECTION[itemCount]; ErrorHandler.ThrowOnFailure(multiSelect.GetSelectedItems(0, itemCount, items)); list.AddRange(items.Select(item => item.pHier)); } return(list); } finally { if (hierarchyPointer != IntPtr.Zero) { Marshal.Release(hierarchyPointer); } if (ppSC != IntPtr.Zero) { Marshal.Release(ppSC); } } }
private IEnumerable<string> GetSelectedFileFullPaths() { IVsMultiItemSelect multiItemSelect; uint itemId; IntPtr hierarchyPtr; IntPtr selectionContainerPtr; var currentSelectionResult = (GetGlobalService(typeof (SVsShellMonitorSelection)) as IVsMonitorSelection). GetCurrentSelection(out hierarchyPtr, out itemId, out multiItemSelect, out selectionContainerPtr); if (ErrorHandler.Failed(currentSelectionResult) || hierarchyPtr == IntPtr.Zero || itemId == VSConstants.VSITEMID_NIL || itemId == VSConstants.VSITEMID_ROOT) { return Enumerable.Empty<string>(); } if (multiItemSelect == null) { return new[] { GetSelectedFileFullPath(itemId, Marshal.GetObjectForIUnknown(hierarchyPtr) as IVsHierarchy) }; } uint selectedItemCount; int pfSingleHirearchy; multiItemSelect.GetSelectionInfo(out selectedItemCount, out pfSingleHirearchy); var selectedItems = new VSITEMSELECTION[selectedItemCount]; multiItemSelect.GetSelectedItems(0, selectedItemCount, selectedItems); return selectedItems.Select(_ => GetSelectedFileFullPath(_.itemid, _.pHier)); }