Logo
HOWTO: Getting information specific to VB.NET and C# projects from an add-in or macro

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: October 2005   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

This article describes how to get information not available in the general extensibility model of Visual Studio .NET (EnvDTE namespace) because it is specific to VB.NET and C# projects.

More Information

Visual Studio .NET supports many kind of projects (VB.NET, C#, C++, setup, database, etc.) and therefore the EnvDTE namespace provided by the EnvDTE.dll assembly only exposes the functionality common to all of them. For example, the information about references of VB.NET and C# projects, which do not apply to other kind of projects, is not exposed by that assembly. The extensibility model specific to VB.NET and C# projects resides in a different assembly named VSLangProj.dll. That assembly can be used in both Visual Studio .NET 2002 and 2003 (the later provides also a VSLangProj2.dll assembly with additional classes). You can get objects of the VSLangProj assembly from the objects of the EnvDTE assembly through its "Object" property. For example, you can cast the property EnvDTE.Project.Object to an VSLangProj.VSProject for VB.NET and C# projects. The following macro shows how to get the information about the references of a VB.NET or C# project:

Sub DisplayProjectReferences()
 
   Dim objProject As EnvDTE.Project
   Dim objVSProject As VSLangProj.VSProject
   Dim objReference As VSLangProj.Reference
   Dim sMsg As String
 
   Try
 
      For Each objProject In DTE.Solution.Projects
 
          If TypeOf objProject.Object Is VSLangProj.VSProject Then

              objVSProject = DirectCast(objProject.Object, VSLangProj.VSProject)
 
              sMsg = "References of project " & objProject.Name & ":" & CrLf & CrLf
 
              For Each objReference In objVSProject.References

                  sMsg &= objReference.Name

                  If objReference.SourceProject Is Nothing Then
                      sMsg &= " " & objReference.Version
                  Else
                      sMsg &= " (Source Project)"
                  End If

                  sMsg &= CrLf

              Next
 
              MessageBox.Show(sMsg)

          End If
 
      Next
 
   Catch objException As System.Exception
      MessageBox.Show(objException.ToString)
   End Try
 
End Sub


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


Top