Logo
HOWTO: Change the build configuration of a project from a Visual Studio add-in

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

This article shows how to change from a Visual Studio 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. The EnvDTE.SolutionContext type has also the ProjectName property to locate the ones of the desired project and the PlatformName property if you want to locate the context for a specific platform.

Note: there is a bug in Visual Studio 2008 and 2010 and the EnvDTE.SolutionContext.ConfigurationName property value doesn't change.

The following add-in, when loaded, changes the ConfigurationName to "Debug" of all the contexts of all the solution 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);

         ShowProjectContexts();

         ChangeProjectContexts(project, "Debug");

         ShowProjectContexts();
      }

      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)
               {
                  solutionContext.ConfigurationName = configurationName;
               }
            }
         }
      }

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

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

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