Get Project References From EnvDTE.Project

Suppose you’re developing a Visual Studio Extension and need to enumerate the references of a Visual Studio project. Here is how to do that:

public static IEnumerable<AssemblyName> CollectSettings(EnvDTE.Project project)
    var vsproject = project.Object as VSLangProj.VSProject;
    // note: you could also try casting to VsWebSite.VSWebSite

    foreach (VSLangProj.Reference reference in vsproject.References)
        if (reference.SourceProject == null)
            // This is an assembly reference
            var fullName = GetFullName(reference);
            var assemblyName = new AssemblyName(fullName);
            yield return assemblyName;
            // This is a project reference

public static string GetFullName(VSLangProj.Reference reference)
    return string.Format("{0}, Version={1}.{2}.{3}.{4}, Culture={5}, PublicKeyToken={6}",
                            reference.MajorVersion, reference.MinorVersion, reference.BuildNumber, reference.RevisionNumber,

For the types in the namespace VSLangProj, you’ll need to reference VSLangProj.dll. EnvDTE.Project ist located in EnvDTE.dll. And just for completness, here’s the extension method used in the code above:

static class Extensions
    public static string Or(this string text, string alternative)
        return string.IsNullOrWhiteSpace(text) ? alternative : text;

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.