3

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() ???
A Coder
  • 3,039
  • 7
  • 58
  • 129

2 Answers2

8

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);
}
Yurii
  • 4,811
  • 7
  • 32
  • 41
  • Where can i find the Microsoft.VisualStudio.Shell.Interop.dll? – A Coder Mar 14 '14 at 07:34
  • On my machine it's under `C:\Program Files (x86)\Microsoft Visual Studio 11.0\VSSDK\VisualStudioIntegration\Common\Assemblies\v2.0` directory. – Yurii Mar 14 '14 at 07:39
  • @SanthoshKumar, you might need to download and install [Visual Studio SDK](http://www.microsoft.com/en-us/download/details.aspx?id=30668). – Yurii Mar 14 '14 at 07:45
0

A quik and dirty solution would be using IndexOf and Substring to find the <ProjectGuid> and </ProjectGuid> string in the .proj-file and extract the content of the tag.

BendEg
  • 20,098
  • 17
  • 57
  • 131