Logo
HOWTO: Add / delete build solution configurations 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 add or delete programmatically from a Visual Studio add-in a build solution configuration.

More Information

To add a build solution configuration you have to use the Add method of the EnvDTE.SolutionConfigurations collection. This method receives as parameters:

  • The new configuration name
  • The name of an existing configuration to copy its settings to the new configuration.
  • A boolean parameter to specify whether to create new build project configurations or not.

To delete a build solution configuration, you have to iterate the EnvDTE.SolutionConfigurations collection, find the first EnvDTE80.SolutionConfiguration2 item whose Name property is the configuration name to delete, and call the SolutionConfiguration2.Delete() method. Note that the EnvDTE.SolutionConfigurations collection can contain different solution configurations with the same Name property but different PlatformName property. Deleting one single configuration deletes all the ones with the same Name for the different platforms.

The following add-in, when loaded, adds and deletes build solution configurations 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()
      {
         EnvDTE80.SolutionBuild2 solutionBuild2;

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

         ShowConfigurations(solutionBuild2);

         AddSolutionConfiguration(solutionBuild2, "UnitTesting", "Debug", false);

         ShowConfigurations(solutionBuild2);

         RemoveSolutionConfiguration(solutionBuild2, "UnitTesting");

         AddSolutionConfiguration(solutionBuild2, "QA", "Debug", true);

         ShowConfigurations(solutionBuild2);
      }

      private void AddSolutionConfiguration(EnvDTE80.SolutionBuild2 solutionBuild2, 
         string newConfigurationName, string existingConfigurationName, bool propagateToProjects)
      {
         solutionBuild2.SolutionConfigurations.Add(newConfigurationName, existingConfigurationName, propagateToProjects);
      }

      private void RemoveSolutionConfiguration(EnvDTE80.SolutionBuild2 solutionBuild2, string configurationName)
      {
         foreach (EnvDTE80.SolutionConfiguration2 solutionConfiguration2 in solutionBuild2.SolutionConfigurations)
         {
            if (solutionConfiguration2.Name == configurationName)
            {
               solutionConfiguration2.Delete();

               // Exit the loop because deleting just one solution configuration with a given name
               // all the configurations with the same name for different platforms are deleted
               break;
            }
         }
      }

      private void ShowConfigurations(EnvDTE80.SolutionBuild2 solutionBuild2)
      {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         EnvDTE.SolutionContexts solutionContexts;

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

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

            solutionContexts = solutionConfiguration2.SolutionContexts;

            foreach (EnvDTE.SolutionContext solutionContext in solutionContexts)
            {
               sb.AppendLine();
               sb.AppendLine("         Project unique name: " + solutionContext.ProjectName);
               sb.AppendLine("         Project configuration: " + solutionContext.ConfigurationName);
               sb.AppendLine("         Project platform: " + solutionContext.PlatformName);
               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 back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top