Logo
HOWTO: Get the solution 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 of the Configuration Manager.

More Information

The automation model (EnvDTE, EnvDTE80) provides the following classes and members to get information about the solution configuration, solution platform and project configurations/platforms for each solution configuration/platform:

  • EnvDTE80.SolutionBuild2 (from EnvDTE.Solution.SolutionBuild), which has the following members (among others):
    • ActiveConfiguration (EnvDTE80.SolutionConfiguration2 type): to get the active solution configuration/solution platform.
    • SolutionConfigurations: to get a linear collection of solution configuration/solution platform items (EnvDTE80.SolutionConfiguration2 type).
  • EnvDTE80.SolutionConfiguration2, which has following members (among others):
    • Name: to get the configuration name (such as "Debug", "Release").
    • PlatformName: to get the platform name (such as "Any CPU"). Note: the old EnvDTE.SolutionConfiguration lacks this property introduced in VS 2005.
    • SolutionContexts: to get the information about the project build configuration/platform for each solution configuration/platform.
    • Activate: to make active the solution configuration/solution platform.
  • EnvDTE.SolutionContext: to get the project build info for a given solution configuration/solution platform. It has the following properties (among others):
    • ProjectName: to get the EnvDTE.Project.UniqueName value (not the EnvDTE.Project.Name value).
    • ConfigurationName: to get the project configuration name.
    • PlatformName: to get the project platform name.
    • ShouldBuild: to get whether the project is built or not.
    • ShouldDeploy: to get whether the project is deployed or not.

The following add-in, when loaded, shows the information of the Configuration Manager for 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()
      {
         ShowSolutionBuildConfigurations();
      }

      private void ShowSolutionBuildConfigurations()
      { 
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         EnvDTE80.Solution2 solution2;
         EnvDTE80.SolutionBuild2 solutionBuild2;
         EnvDTE80.SolutionConfiguration2 activeSolutionConfiguration2;
         EnvDTE.SolutionContexts solutionContexts;
         List<string> usedNames = new List<string>();
         string solutionConfigurationName;
         string solutionPlatformName;

         solution2 = (EnvDTE80.Solution2) _applicationObject.Solution;
         solutionBuild2 = (EnvDTE80.SolutionBuild2) solution2.SolutionBuild;

         // Active solution configuration
         activeSolutionConfiguration2 = (EnvDTE80.SolutionConfiguration2) solutionBuild2.ActiveConfiguration;
         sb.AppendLine("Active solution configuration: " + activeSolutionConfiguration2.Name );
         sb.AppendLine("Active solution platform: " + activeSolutionConfiguration2.PlatformName);

         // Solution configuration names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Solution configuration names:");
         sb.AppendLine();
         usedNames.Clear();

         foreach (SolutionConfiguration2 solutionConfiguration2 in solutionBuild2.SolutionConfigurations)
         {
            solutionConfigurationName = solutionConfiguration2.Name;

            if (!usedNames.Contains(solutionConfigurationName))
            {
               usedNames.Add(solutionConfigurationName);
               sb.AppendLine("   - " + solutionConfigurationName);
            }
         }

         // Solution platform names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Solution platform names:");
         sb.AppendLine();
         usedNames.Clear();
         
         foreach (SolutionConfiguration2 solutionConfiguration2 in solutionBuild2.SolutionConfigurations)
         {
            solutionPlatformName = solutionConfiguration2.PlatformName;

            if (!usedNames.Contains(solutionPlatformName))
            {
               usedNames.Add(solutionPlatformName);
               sb.AppendLine("   - " + solutionPlatformName);
            }
         }

         // Solution configurations/platforms
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Project contexts for each solution configuration/platform:");

         foreach (SolutionConfiguration2 solutionConfiguration2 in solutionBuild2.SolutionConfigurations)
         {
            sb.AppendLine();

            sb.AppendLine("   - Solution configuration: " + solutionConfiguration2.Name + 
               ", solution platform: " + solutionConfiguration2.PlatformName);

            solutionContexts = solutionConfiguration2.SolutionContexts;
         
            foreach (EnvDTE.SolutionContext solutionContext in solutionContexts)
            {
               sb.AppendLine();
               sb.Append("         * Project unique name: " + solutionContext.ProjectName);
               sb.Append(", project configuration: " + solutionContext.ConfigurationName);
               sb.Append(", project platform: " + solutionContext.PlatformName);
               sb.Append(", project should build: " + solutionContext.ShouldBuild.ToString());
               sb.Append(", project should deploy: " + solutionContext.ShouldDeploy.ToString());
               sb.AppendLine();
            }
         }

         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 to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top