Convert IVsHierarchy to ProjectItem or Project

The title says it all… here you go:

Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hierarchy = // ... get it.

// VSITEMID_ROOT gets the current project. 
// Alternativly, you might have another item id.
var itemid = VSConstants.VSITEMID_ROOT; 

object objProj;
hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out objProj);

var projectItem = objProj as EnvDTE.ProjectItem;
// ... or ...
var project = objProj as EnvDTE.Project;

Thanks to Ed Dore in the Visual Studio forums.

Freelance full-stack .NET and JS developer and architect. Located near Cologne, Germany.

3 thoughts on “Convert IVsHierarchy to ProjectItem or Project

Leave a Reply

Your email address will not be published. Required fields are marked *