2017-07-18 1 views
0

J'écris une extension pour Visual Studio 2017, l'extension est disponible dans le menu contextuel d'un projet (par un clic droit, etc.) en utilisantExtensions Visual Studio Obtenir le chemin du projet

IDM_VS_CTXT_PROJNODE 

Ma question est quand Je tape

private void MenuItemCallback(object sender, EventArgs e)  

comment puis-je obtenir le chemin du projet?

Répondre

1

Veuillez vérifier le code suivant, qui utilise le service SVsShellMonitorSelection, vous pouvez obtenir une référence à la hiérarchie sélectionnée comme IVsHierarchy, ce qui me permet d'obtenir une référence à l'objet sélectionné. Cela peut ensuite être converti en classes telles que Project, ProjectItem, etc., en fonction de ce qui est sélectionné dans l'Explorateur de solutions.

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

Cela fait bien le travail. Je vous remercie. – SuperAaz