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
      Microsoft Visual Studio 2005

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)
  • Web Applications (Visual Studio 2005 SP1)
  • VSA projects
  • Enterprise Projects  (Visual Studio .NET 2002/2003)
  • Setup projects
  • Smart Device projects
  • VSTO projects (Visual Studio 2005)

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:

Public Function ProjectHasExtender(ByVal proj as EnvDTE.Project, ByVal extenderName As String) As Boolean
   Dim bResult As Boolean = False
   Dim colExtenderNames() As Object
   Dim objExtenderName 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:

       colExtenderNames = DirectCast(proj.ExtenderNames, Object())
       If colExtenderNames.Length > 0 Then
          For Each objExtenderName In colExtenderNames
             If objExtenderName.ToString = extenderName Then
                bResult = True
                Exit For
             End If
          Next
      End If
   Catch objException As Exception
      ' Ignore
   End Try
   Return bResult
End Function

Some well-known extender names are:

  • "VST" for VSTO projects (Visual Studio 2005)
  • "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 Web Applications (see below).

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 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 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

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top