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: Get the language of a project or file from a Visual Studio .NET macro or add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: January 2006   Microsoft Visual Studio .NET 2003
      Microsoft Visual Studio 2005
Introduction

This article describes how to get the .NET language of a project or file from a Visual Studio .NET macro or add-in.

More Information

The extensibility model of Visual Studio .NET provides the EnvDTE.Project class to represent a project, and its Project.CodeModel property returns an EnvDTE.CodeModel object representing the code model of the whole project. The EnvDTE.CodeModel class has a Language property to return the language of the project. In a similar way, the EnvDTE.ProjectItem class represents a project item (normally a file), its ProjectItem.FileCodeModel property returns an EnvDTE.FileCodeModel object representing the code model of the file, and the FileCodeModel.Language property returns the language of the file.

Since .NET is an open platform which allows multiple languages (anyone can add a new .NET language), there is no enum value to group the languages together. Instead, each language is identified by a GUID string. This way the platform can accommodate languages that are not created yet. The extensibility model provides the EnvDTE.CodeModelLanguageConstants class to group some known language GUIDs, but in some cases you will have to hardcode the values of the GUIDs for other languages.

The following macro illustrates these concepts:

Sub GuessNetLanguage()
 
   Dim sLanguageGuid As String
 
   Try
 
      ' Get the language of a project
      'sLanguageGuid = DTE.ActiveDocument.ProjectItem.ContainingProject.CodeModel.Language
 
      ' Get the language of a file
      sLanguageGuid = DTE.ActiveDocument.ProjectItem.FileCodeModel.Language
 
      Select Case sLanguageGuid
 
         Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageVB
            MessageBox.Show("Visual Basic .NET")
 
         Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageCSharp
            MessageBox.Show("C#")
 
         Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageVC
            MessageBox.Show("Visual C++")
 
         Case EnvDTE.CodeModelLanguageConstants.vsCMLanguageMC
            MessageBox.Show("Managed C++")
 
         Case EnvDTE80.CodeModelLanguageConstants2.vsCMLanguageJSharp ' Only Visual Studio 2005
            MessageBox.Show("Visual J#")
 
      End Select
 
   Catch objException As System.Exception
      MessageBox.Show(objException.ToString)
   End Try
 
End Sub

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