2017-07-18 51 views
0

我写的Visual Studio 2017年的延伸,扩展是在项目的上下文菜单(右键单击等)使用的Visual Studio扩展获取项目路径

IDM_VS_CTXT_PROJNODE 

我的问题是,当我输入

private void MenuItemCallback(object sender, EventArgs e)  

如何获取项目路径?

回答

1

请检查以下代码,它使用SVsShellMonitorSelection服务,您可以获取对所选层次结构的引用作为IVsHierarchy,这反过来又允许我获取对所选对象的引用。然后,可以根据Solution Explorer中选择的内容将其转换为诸如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); 
     } 
+0

这很好地完成了这项工作。谢谢。 – SuperAaz

相关问题