Logo
BUG: Build configuration of a project cannot be changed from a Visual Studio add-in

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

This article shows a problem changing from a Visual Studio 2008/2010 add-in the build configuration of a project for a given a build solution configuration.

More Information

To change the configuration of a project you need to assign the EnvDTE.SolutionContext.ConfigurationName property (which is writable) for the desired EnvDTE.SolutionContext of the EnvDTE.SolutionConfiguration.SolutionContexts collection. However, for Visual Studio 2008 and 2010 the EnvDTE.SolutionContext.ConfigurationName property value doesn't change using an existing (different) configuration or causes an exception if you just added a new project configuration using ConfigurationManager.AddConfigurationRow before changing the configuration name to that new configuration. It works correctly in Visual Studio 2005 and 2012.

The following add-in reproduces the problem:

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

         ChangeProjectContexts(project, "Debug");

      }

      private void ChangeProjectContexts(EnvDTE.Project project, string configurationName)
      {
         EnvDTE.SolutionConfigurations solutionConfigurations;

         solutionConfigurations = _applicationObject.Solution.SolutionBuild.SolutionConfigurations;

         foreach (EnvDTE80.SolutionConfiguration2 solutionConfiguration2 in solutionConfigurations)
         {
            foreach (EnvDTE.SolutionContext solutionContext in solutionConfiguration2.SolutionContexts)
            {
               if (solutionContext.ProjectName == project.UniqueName)
               {
                  // Change the configuration name if it is not the desired one
                  if (solutionContext.ConfigurationName != configurationName)
                  {
                     solutionContext.ConfigurationName = configurationName;

                     if (solutionContext.ConfigurationName == configurationName)
                     {
                        // This is the case of VS 2005 and 2012
                        MessageBox.Show("SolutionContext.ConfigurationName changed");
                     }
                     else
                     {
                        // This is the case of VS 2008 and 2010
                        MessageBox.Show("SolutionContext.ConfigurationName NOT changed");
                     }
                  }
               }
            }
         }
      }

      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