HOWTO: Get the target .NET Framework of a Visual Studio 2008 project from a Visual Studio add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2008
Date: June 2008   Microsoft Visual Studio 2010
Updated: March 2013   Microsoft Visual Studio 2012

Projects created with Visual Studio versions previous to Visual Studio 2008 target a single .NET Framework version:

  • Visual Studio .NET 2002 targets .NET Framework 1.0
  • Visual Studio .NET 2003 targets .NET Framework 1.1
  • Visual Studio 2005 targets .NET Framework 2.0

Visual Studio 2008 (and higher) projects can target .NET Framework 2.0, .NET Framework 3.0 and .NET Framework 3.5, etc.

You can specify the target .NET Framework:

  • When creating the project, in the New Project dialog window, which offers a combobox to select the target .NET Framework.
  • Afterwards, in the Project Properties dialog, "Compile" section, "Advanced Compile Options" button.

This article explains how to get programmatically the target .NET Framework of an EnvDTE.Project from a Visual Studio macro or add-in.

More Information

To get the target .NET Framework of a Visual Studio 2008 (or higher) project you must get the "TargetFramework" property from the EnvDTE.Project.Properties collection. The returned value is an unsigned integer which apparently doesn't make sense. However, when you convert it to hexadecimal notation, it can be 0x20000, 0x30000 or 0x30005, etc. representing the .NET Framework 2.0, 3.0 or 3.5.

The following macro shows the source code to get target .NET Framework:

Sub Test()


End Sub

Function GetTargetFramework(ByVal objProject As EnvDTE.Project) As String

   Dim iValue As Integer

   iValue = CType(objProject.Properties.Item("TargetFramework").Value, Integer)

   Return iValue.ToString("X")

End Function

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)