Logo
HOWTO: Add / delete build project 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 project configuration.

More Information

To add a build project configuration you have to use the AddConfigurationRow method of the EnvDTE.ConfigurationManager type (that you can get from the EnvDTE.Project.ConfigurationManager property). 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 solution configuration or not.

To delete a build project configuration, you have to use the DeleteConfigurationRow method of the same EnvDTE.ConfigurationManager type.

The following add-in, when loaded, adds and deletes build project configurations 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()
      {
         EnvDTE.Project project;

         project = _applicationObject.Solution.Projects.Item(1);

         ShowConfigurations(project);

         AddProjectConfiguration(project, "UnitTesting", "Debug", false);

         ShowConfigurations(project);

         DeleteProjectConfiguration(project, "UnitTesting");

         AddProjectConfiguration(project, "QA", "Debug", true);

         ShowConfigurations(project);
      }

      private void AddProjectConfiguration(EnvDTE.Project project, string newConfigurationName, 
         string existingConfigurationName, bool propagateToSolution)
      {
         project.ConfigurationManager.AddConfigurationRow(newConfigurationName, existingConfigurationName, propagateToSolution);
      }

      private void DeleteProjectConfiguration(EnvDTE.Project project, string configurationName)
      {
         project.ConfigurationManager.DeleteConfigurationRow(configurationName);
      }

      private void ShowConfigurations(EnvDTE.Project project)
      {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         EnvDTE80.SolutionBuild2 solutionBuild2;
         List<string> usedNames = new List<string>();
         string solutionConfigurationName;
         solutionBuild2 = (EnvDTE80.SolutionBuild2)_applicationObject.Solution.SolutionBuild;

         // 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);
            }
         }

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

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

         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