| Author: |
Carlos J. Quintero (Microsoft MVP) |
Applies to: |
Microsoft Visual Studio .NET 2002 |
| Date: |
February 2008 |
|
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 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 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
|