Search code examples
c#.netenvdte

Get the GUID of .proj file using C#


I need to get the GUID of a C# .proj file using C#. I have taken from the .proj file like reading the file using XML and getting the tag value.

//XML Tag

 <ProjectGuid>{7701AEB4-8549-4FB2-B34C-E71D0B7DE59D}</ProjectGuid>

But i need to know is there any specific property in C# to retrieve the existing .proj file GUID, something like this.

//Code

 int iIncriment = 0;
 foreach (var objProj in vhaSolution.Projects)
        {
          EnvDTE.Project prj1 = (EnvDTE.Project)objProj;
          iIncriment++;
            if (string.Equals(testProjectName + ".proj", prj1.Name))
                {
                    prj = vhaSolution.Projects.Item(iIncriment);
                }
        }

 string GUID = prj.GetGUID() ???

Solution

  • You can use the IVsHierarchy.GetGuidProperty method:

    var solution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
    IVsHierarchy hierarchy;
    
    solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
    
    if (hierarchy != null)
    {
        Guid projectGuid;
    
        hierarchy.GetGuidProperty(
                    VSConstants.VSITEMID_ROOT,
                    (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
                    out projectGuid);
    }