Getting a project’s GUID

OK, this was much harder than it should have been. I was in the process of creating my own custom Silverlight project templates (I don’t consider the default template usable since it requires you to put your business entities in the host web project!).

Anyway, the host web project needs to get a reference to the Silverlight project–specifically the project’s GUID. Here are the steps you need to take to get this information:

  1. Install the Visual Studio 2010 SDK: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=47305cf4-2bea-43c0-91cd-1b853602dcc5&displaylang=en
  2. Add these assemblies to your project:

    – Microsoft.VisualStudio.Shell.Interop
    – Microsoft.VisualStudio.OLE.Interop

  3. Now you can use the following code to retrieve a project’s GUID:

     

    Guid projectGuid= Guid.Empty;

     

    IVsHierarchy hierarchy;

     

    IServiceProvider serviceProvider = new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);

     

    IVsSolution solution = serviceProvider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsSolution)) as IVsSolution;

    solution.GetProjectOfUniqueName(project.FullName,

    out hierarchy);

     

    if (hierarchy != null)
    {
       solution.GetGuidOfProject(hierarchy,
    out projectGuid);
    }

Kevin McNeish
President, Oak Leaf Enterprises, Inc.
INETA Speaker
Eight-time recipient Microsoft .NET MVP Award
Chief Software Architect, MM .NET Application Framework

One thought on “Getting a project’s GUID

  1. Hi Kevin,

    Where did you put this code? Did you place it in your IWizard implementation..maybe in the ProjectItemFinishedGenerating method?

    Regards

    Myles

Leave a Reply to Myles J Cancel reply

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