http://blogs.clariusconsulting.net/pga

Pablo Galiano's Blog

Go Back to
pga′s Latest post

How do I obtain a project GUID

My twelfth How do I is up.

Scenario

Let’s say that we have a DTE project instance and we want to get the project GUID. If we look carefully the EnvDTE.Project interface doesn’t have a guid property. To obtain it we need to first obtain the corresponding IVsHierarchy for the project and then query the guid property. The focus of this How do I tackles this scenario.

 

Interfaces and classes needed

 

Code snippet

    public Guid GetProjectGuid(IServiceProvider serviceProvider, Project project)

    {

        var solution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;

 

        IVsHierarchy hierarchy;

 

        solution.GetProjectOfUniqueName(project.FullName, out hierarchy);

 

        if(hierarchy != null)

        {

            Guid projectGuid;

 

            ErrorHandler.ThrowOnFailure(

                hierarchy.GetGuidProperty(

                    VSConstants.VSITEMID_ROOT,

                    (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,

                    out projectGuid));

 

            if(projectGuid != null)

            {

                return projectGuid;

            }

        }

 

        return Guid.Empty;

    }

Assemblies needed

  • Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

 

Stay tuned,

Pablo

Comments

1 Comment

  1. YUb8bervur nurnvuirenbv niu