Logo
HOWTO: Get the project configurations / platforms from a Visual Studio add-in

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

This article shows how to get programmatically from a Visual Studio add-in the information about the build configurations / platforms of a given project.

More Information

The automation model (EnvDTE) provides the following classes and members to get information about the configuration names, platform names, and project configuration/platform combinations defined for a given EnvDTE.Project:

  • EnvDTE.ConfigurationManager (from EnvDTE.Project.ConfigurationManager), which has the following members (among others):
    • ConfigurationRowNames: to get the names of the configurations (such as "Debug", "Release") defined for the project.
    • PlatformNames: to get the names of the platforms (such as "Any CPU") defined for the project.
    • SupportedPlatformNames: to get the names of the available platforms (such as "Any CPU", "x86", "x64").
    • ConfigurationRow(configurationName): for the given configuration name (such as "Debug"), returns a collection of EnvDTE.Configuration objects (one for each platform name such as "Any CPU") .
    • Platform(platformName): for the given platform name (such as "Any CPU"), returns a collection of EnvDTE.Configuration objects (one for each configuration name such as "Debug", "Release").
  • EnvDTE.Configuration, which has following members (among others):
    • ConfigurationName: the configuration name (such as "Debug", "Release").
    • PlatformName: to platform name (such as "Any CPU").
    • Properties, IsRunnable, IsDeployable, etc: to get the information about the configuration.

The following add-in, when loaded, shows the information of the configuration / platforms for the first project of the currently loaded solution:

Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Collections.Generic;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

namespace MyAddin1
{
   public class Connect : IDTExtensibility2
   {
      private DTE2 _applicationObject;
      private AddIn _addInInstance;
      
      public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
      {
         _applicationObject = (DTE2)application;
         _addInInstance = (AddIn)addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_Startup:
               // Do nothing OnStartupComplete will be called
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:
               InitializeAddIn();
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         ShowProjectBuildConfigurations(_applicationObject.Solution.Projects.Item(1));
      }

      private void ShowProjectBuildConfigurations(EnvDTE.Project project)
      { 
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         EnvDTE.ConfigurationManager projectConfigurationManager;
         EnvDTE.Configurations projectConfigurations;

         projectConfigurationManager = project.ConfigurationManager;

         // Project configuration names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Project configuration names:");
         sb.AppendLine();

         foreach (object projectConfigurationName in (object[]) projectConfigurationManager.ConfigurationRowNames)
         {
            sb.AppendLine("   - " + projectConfigurationName.ToString());
         }

         // Project platform names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Project platform names:");
         sb.AppendLine();
         
         foreach (object projectPlatformName in (object[]) projectConfigurationManager.PlatformNames)
         {
            sb.AppendLine("   - " + projectPlatformName.ToString());
         }

         // Supported project platform names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Supported project platform names:");
         sb.AppendLine();

         foreach (object projectPlatformName in (object[]) projectConfigurationManager.SupportedPlatforms)
         {
            sb.AppendLine("   - " + projectPlatformName.ToString());
         }

         // Project configurations by configuration
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Project configurations by configuration:");
         sb.AppendLine();

         foreach (object projectConfigurationName in (object[]) projectConfigurationManager.ConfigurationRowNames)
         {
            projectConfigurations = projectConfigurationManager.ConfigurationRow(projectConfigurationName.ToString());

            foreach (EnvDTE.Configuration projectConfiguration in projectConfigurations)
            {
               sb.AppendLine("   - Project configuration: " + projectConfiguration.ConfigurationName + 
                  ", project platform: " + projectConfiguration.PlatformName);
            }
         }

         // Project configurations by platform
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Project configurations by platform:");
         sb.AppendLine();

         foreach (object projectPlatformName in (object[])projectConfigurationManager.PlatformNames)
         {
            projectConfigurations = projectConfigurationManager.Platform(projectPlatformName.ToString());

            foreach (EnvDTE.Configuration projectConfiguration in projectConfigurations)
            {
               sb.AppendLine("   - Project platform: " + projectConfiguration.PlatformName +
                  ", project configuration: " + projectConfiguration.ConfigurationName);
            }
         }

         MessageBox.Show(sb.ToString());

      }

      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }

      public void OnBeginShutdown(ref Array custom)
      {
      }
      
   }
}

Related articles


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top