Visual Studio扩展获取项目路径

4

我正在为Visual Studio 2017编写一个扩展程序,该扩展程序可以通过项目的上下文菜单(右键单击等方式)使用。

IDM_VS_CTXT_PROJNODE

我的问题是当我输入 "

" 时,浏览器会发生什么情况?

private void MenuItemCallback(object sender, EventArgs e)       

如何获取项目路径?

1个回答

8
请检查以下代码,使用SVsShellMonitorSelection服务可以获得选定层次结构的IVsHierarchy引用,然后我可以获取选定对象的引用。根据在解决方案资源管理器中选择的内容,可以将其转换为Project、ProjectItem等类。
private void MenuItemCallback(object sender, EventArgs e)
        {
            string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            string title = "ItemContextCommand";

            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;

            string projectPath = selectedProject.FullName;

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.ServiceProvider,
                message,
                projectPath,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }

这个很好地完成了工作。谢谢。 - Aazarus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接