Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Guess the type of a Visual Studio project from an add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: February 2007   Microsoft Visual Studio .NET 2003
Updated: March 2009   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008

Introduction

This article provides information about how to guess the type of a project (Windows, executable or class library, Web Site, Web application, etc.) from a Visual Studio macro or add-in. You can have, at least, the following project types:

  • Windows applications (Windows executable, class library or console executable)
  • Web Applications (Visual Studio .NET 2002/2003)
  • Web Sites (Visual Studio 2005 and higher)
  • Web Applications (Visual Studio 2005 SP1 and higher)
  • VSA projects
  • Enterprise Projects  (Visual Studio .NET 2002/2003)
  • Setup projects
  • Smart Device projects
  • Visual Studio Tools for Office (VSTO) projects (Visual Studio 2005 and higher)
  • XNA projects (XBox, Zune, Windows games)
  • Silverlight projects (Visual Studio 2008 and higher)

More Information

The first property to use to determine the type of a project is EnvDTE.Project.Kind, which returns a Guid (since Visual Studio is so extensible, it must accommodate unknown project types and therefore it can not be an enum). You must add some assembly references such as VSLangProj.dll, etc. to your project to get the definition of some well-known Guid values and other classes or interfaces. See the article INFO: Assemblies used in Visual Studio Extensibility.

Among others, EnvDTE.Project.Kind can take these values:

  • VSLangProj.prjKindVBProject for VB.NET projects.
  • VSLangProj.prjKindCSharpProject for C# projects.
  • VSLangProj.prjKindVSAProject for VSA projects (macros projects).
  • VSLangProj2.prjKindVJSharpProject for Visual J# projects.
  • VSLangProj2.prjKindSDEVBProject for VB.NET Smart Device projects (Visual Studio .NET 2002/2003 only, see below for Visual Studio 2005).
  • VSLangProj2.prjKindSDECSharpProject for C# projects  (Visual Studio .NET 2002/2003 only, see below for Visual Studio 2005).
  • {7D353B21-6E36-11D2-B35A-0000F81F0C06} for "Enterprise Projects" (Visual Studio .NET 2002/2003 only).
  • {54435603-DBB4-11D2-8724-00A0C9A8B90C} for Setup projects.
  • EnvDTE.Constants.vsProjectKindSolutionItems for the Solution Items folder of the Solution Explorer.
  • EnvDTE.Constants.vsProjectKindMisc for the Miscellaneous Files folder of the Solution Explorer.

Also, you can check the availability of project extenders to guess the type of a project. You can check the presence of an extender in a project with this function:

Language: VB.NET   Copy Code Copy Code (IE only)
Public Function ProjectHasExtender(ByVal proj as EnvDTE.Project, ByVal extenderName As String) As Boolean

   Dim result As Boolean = False
   Dim extenderNames() As Object
   Dim extenderNameObject As Object

   Try
       ' We could use proj.Extender(extenderName) but it causes an exception if not present and 
       ' therefore it can cause performance problems if called multiple times. We use instead:

       extenderNames = DirectCast(proj.ExtenderNames, Object())

       If extenderNames.Length > 0 Then

          For Each extenderNameObject In colExtenderNames

             If extenderNameObject.ToString() = extenderName Then
                result = True
                Exit For
             End If

          Next

      End If

   Catch 
      ' Ignore
   End Try

   Return result

End Function
Language: C#   Copy Code Copy Code (IE only)
public bool ProjectHasExtender(EnvDTE.Project proj, string extenderName)
{
   bool result = false;
   object[] extenderNames;

   try
   {
      // We could use proj.Extender(extenderName) but it causes an exception if not present and 
      // therefore it can cause performance problems if called multiple times. We use instead:

      extenderNames = (object[]) proj.ExtenderNames;

      if (extenderNames.Length > 0)
      {
         foreach (object extenderNameObject in extenderNames)
         {
            if (extenderName.ToString() == extenderName)
            {
               result = true;
               break;
            }
         }
      }
   }
   catch
   {
      // Ignore
   }
   return result;
}

Some well-known extender names are:

  • "VST" for VSTO projects (Visual Studio 2005 and higher). You can distinguish between VSTO Excel, Word or Outlook projects checking the presence of some specific reference in the project such as "Microsoft.Office.Tools.Word", "Microsoft.Office.Tools.Excel", ""Microsoft.Office.Tools.Outlook". To learn how to get the references of a project see HOWTO: Getting information specific to VB.NET and C# projects from an add-in or macro.
  • "SDEProjectExtender" for Smart Device projects of Visual Studio 2005. The ones for Visual Studio .NET 2002/2003 are obtained as described above.
  • "WebApplication" for Visual Studio 2005 SP1 or Visual Studio 2008 Web Applications (see below).
  • "SilverlightProject" for Silverlight projects.
  • "Microsoft.Xna.GameStudio.CodeProject.ParentProjectExtender" for XNA projects. You can distinguish between XBox, Zune and Windows game projects getting the active configuration (EnvDTE.Project.ConfigurationManager.ActiveConfiguration), getting the "DefineConstants" property in the EnvDTE.Configuration.Properties collection and checking if its Value property contains the "XBOX", "ZUNE" or "WINDOWS" words.

You can distinguish Windows from Web applications using the EnvDTE.Project.Properties.Item("ProjectType") property, which can take these values:

  • VSLangProj.prjProjectType.prjProjectTypeLocal for Windows applications.
  • VSLangProj.prjProjectType.prjProjectTypeWeb for non-Windows (Web) applications, with the following caveats:

    - Web Sites of Visual Studio 2005 and higher can be discovered checking if the value returned by the property EnvDTE.Project.Object is of the VsWebSite.VSWebSite type.

    - Web Applications of Visual Studio 2005 SP1 and higher return VSLangProj.prjProjectType.prjProjectTypeLocal due to a bug. You can discover them checking that they are not a Web Site and that they have the extender "WebApplication" (see above).

For Windows applications you can guess the type using the property EnvDTE.Project.Properties.Item("OutputType"), which can take these values:

  • VSLangProj.prjOutputType.prjOutputTypeWinExe for Windows executables.
  • VSLangProj.prjOutputType.prjOutputTypeLibrary for class libraries.
  • VSLangProj.prjOutputType.prjOutputTypeExe for console executables.

To guess the .NET language of a project see the article HOWTO: Get the language of a project or file from a Visual Studio .NET macro or add-in.

The ultimate way of guessing the type / subtype of a project is to get its Guids. See the article HOWTO: Get the project flavour (subtype) of a Visual Studio project from an add-in.

Related articles

 


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


   Top