none
Getting a full path file name from the IVsHierarchy service

    Question

  •  

    Hi all,

    I went over the SolutionHierarchyTraversal in the DSK samples, where there is code there for going over all the files in the currently loaded Solution in the Solution Explorer window.

    I saw that the GetProperty method over the hierarchy, with the property VSHPROPID_Name, does not return the full path name of the file. (e.g "C\Program Files\xx.txt", but only "xx.txt").

    Is there a different property i should call to retrieve this information?

    (I tried VSHPROPID_ProjectDir and got a "Not implemented" HResult).

     

    10x a lot in advance,

    Michal.

    Sunday, February 17, 2008 8:41 AM

Answers

  • Hi Michal,

    I know that Gaston Milano does it in his add-in named Cool Commands for opening Windows Explorer in folder where project or project item is. I suspect he combines the VSHPROPID_Name of item with some path from its parent project.

    Maybe it makes sense to contact him with your question.

    Hope that helps,

    Sunday, February 17, 2008 10:52 AM

All replies

  • Hi Michal,

    I know that Gaston Milano does it in his add-in named Cool Commands for opening Windows Explorer in folder where project or project item is. I suspect he combines the VSHPROPID_Name of item with some path from its parent project.

    Maybe it makes sense to contact him with your question.

    Hope that helps,

    Sunday, February 17, 2008 10:52 AM
  •  

    The code for the OpenProjectPath is really simple, I did it using automation.

     

    Here is the code (without error checking) :

     

    Code Snippet

    private void OnExploreProjectPath()

    {

       SelectedItems items = DTE.SelectedItems;

       Selected item = items.Item(1);

       if (item.Project != null)

          OpenFolder(item.Project.FullName);

       else

       {

            ProjectItem pItem = item.ProjectItem;

            OpenFolder(pItem.get_FileNames(0));

      }

    }

     

     

     

    If you have IVsHierarchy and you want a EnvDTE.Project you could use the following function

     

    Code Snippet

    public EnvDTE.Project ToDteProject(IVsHierarchy hierarchy)

    {

      object prjObject = null;

      if (hierarchy.GetProperty(Oxfffffffe, -2007, out prjObject) >= 0)

          return (EnvDTE.Project) prjObject;

      throw new ArgumentException("Invalid hierarchy, the hierrarchy is not a project");

    }

     

     

     

    Hope this help.

    Regards,

    Gastón Milano

     

     

    Monday, February 18, 2008 2:03 PM
  • This doesn't appear to work for selected folders in a Visual C++ project, as the ProjectItem field is null.
     
    Any ideas on how to get the full path for the selected folder in solution explorer?
    Tuesday, July 15, 2008 9:05 PM
  • Got it.

     
    // Subscribe to the SVsShellMonitorSelection service somewhere:  
       public void mySetupMethod()  
       {  
             IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Package.GetGlobalService(typeof(SVsShellMonitorSelection));     
             monitorSelection.AdviseSelectionEvents(thisout cookie);    
       }  
     
    // This class (as used in AdviseSelection) must implement the IVsSelectionEvents interface  
    // To get the folder path use the following  
       public int OnSelectionChanged(IVsHierarchy pHierOld, uint itemidOld,   
          IVsMultiItemSelect pMISOld, ISelectionContainer pSCOld,   
          IVsHierarchy pHierNew, uint itemidNew, IVsMultiItemSelect pMISNew, ISelectionContainer pSCNew)  
       {  
          string fullPath;  
     
          // Get the full path
          pHierNew.GetCanonicalName(itemidNew, out fullPath);  
     
          // Do something with the path...  
     
          return VSConstants.S_OK;  
       }  
     
     
     

    Alternatively, this works:
    1) Register an event handler with the DTE.Events.SelectionEvents.OnChange event.
    2) In the event handler, obtain an IVsMonitorSelection interface (or keep one handy) and call GetCurrentSelection on it.
    3) Cast the hierPtr to an IVsHierarchy:      IVsHierarchy hier = Marshal.GetObjectForIUnknown(hierPtr) as IVsHierarchy;
    4) Call IVsHierarchy.GetCanonicalName on this interface as shown in sample, using the ID obtained from the GetCurrentSelection.
    • Edited by thethirdhalf Wednesday, July 16, 2008 2:11 PM Redundant line in code.
    Wednesday, July 16, 2008 1:51 PM