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 if a project configuration is Debug or Release from a Visual Studio add-in or macro

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

This article explains how to guess programmatically (from inside an add-in or macro) whether a project configuration is Debug or Release.

More Information

Given an EnvDTE.Project, you can get its configurations using the Project.ConfigurationManager. For example, you can get the active configuration for the project using Project.ConfigurationManager.ActiveConfiguration, which returns an EnvDTE.Configuration object. While you can get the configuration name using the Configuration.Name property, you can't rely on the default names such as "Debug" or "Release" because other configurations can have another names. For this purpose, you have to use the Configuration.Properties collection.

One relevant property is "DefineDebug", which apparently should be enough for this purpose, but unfortunately only works for VB.NET projects, while for C# projects in VS 2005 or 2008 always returns False (it appears to be a bug since in VS.NET 2002 or 2003 works as expected). For C# projects you can use another property, "DefineConstants" which returns the constants (such as "DEBUG", etc.) defined for the configuration. While VB.NET projects remove the "DEBUG" constant from this value and initialize correctly the "DefineDebug" property, C# 2005 or 2008 projects leave the "DefineConstants" value intact and don't initialize the "DefineDebug" property value.  Therefore, you have to detect the language and for VB.NET projects use the "DefineDebug" property, while for C# projects you have to parse the "DefineConstants" property to see if it includes the "DEBUG" value.

The following macro shows the problem for VS 2005 and 2008 and the workaround:
Sub ProjectConfigurations()

   Dim objProject As Project
   Dim objConfiguration As Configuration
   Dim sDefineConstants As String
   Dim bUnreliableIsDebug As Boolean
   Dim bReliableIsDebug As Boolean
   Dim sMsg As String

   For Each objProject In DTE.Solution.Projects

      objConfiguration = objProject.ConfigurationManager.ActiveConfiguration

      sDefineConstants = objConfiguration.Properties.Item("DefineConstants").Value.ToString()
      bUnreliableIsDebug = DirectCast(objConfiguration.Properties.Item("DefineDebug").Value, Boolean)

      sMsg = "Project: " & objProject.Name & ControlChars.CrLf
      sMsg &= "DefineConstants: " & sDefineConstants & ControlChars.CrLf

      Select Case objProject.CodeModel.Language
         Case "{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}"
            sMsg &= "Language: VB.NET" & ControlChars.CrLf
            bReliableIsDebug = bUnreliableIsDebug
         Case "{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}"
            sMsg &= "Language: C#" & ControlChars.CrLf
            bReliableIsDebug = (sDefineConstants.IndexOf("DEBUG") >= 0)
      End Select

      sMsg &= "Unreliable IsDebug: " & bUnreliableIsDebug & ControlChars.CrLf
      sMsg &= "Reliable IsDebug: " & bReliableIsDebug & ControlChars.CrLf

      MessageBox.Show(sMsg)

   Next

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