Logo
HOWTO: Get or set the active solution configuration/platform 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 or set programmatically from a Visual Studio add-in the active solution build configuration / platform.

More Information

To get the active solution build configuration / platform you can use EnvDTE.Solution.SolutionBuild.ActiveConfiguration. Since this property returns an EnvDTE.SolutionConfiguration object that lacks a property to get the platform, you need to cast the result to the EnvDTE80.SolutionConfiguration2 type that has a PlatformName property.

To set the active solution build configuration / platform, you need to iterate the solution configurations of the EnvDTE.Solution.SolutionBuild.SolutionConfigurations collection (using EnvDTE80.SolutionConfiguration2, not EnvDTE.SolutionConfiguration), find the one with the desired configuration name and platform name, and then call the Activate() method.

The following add-in, when loaded, shows the solution configuration / platform of the currently loaded solution and then changes it to "Release" / "Any CPU":

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()
      {
         ChangeActiveConfigurationAndPlatform("Release", "Any CPU");
      }

      private void ChangeActiveConfigurationAndPlatform(string configurationName, string platformName)
      { 
         EnvDTE80.SolutionConfiguration2 solutionConfiguration2 = null;

         solutionConfiguration2 = (EnvDTE80.SolutionConfiguration2)_applicationObject.Solution.SolutionBuild.ActiveConfiguration; 

         MessageBox.Show("The old configuration was: Configuration Name: " + solutionConfiguration2.Name +
            ", Platform Name: " + solutionConfiguration2.PlatformName);

         foreach (EnvDTE80.SolutionConfiguration2 solConfiguration2 in _applicationObject.Solution.SolutionBuild.SolutionConfigurations)
         {
            if (solConfiguration2.Name == configurationName && solConfiguration2.PlatformName == platformName)
            {
               solConfiguration2.Activate();
               break;
            }
         }

         solutionConfiguration2 = (EnvDTE80.SolutionConfiguration2)_applicationObject.Solution.SolutionBuild.ActiveConfiguration; 

         MessageBox.Show("The new configuration is: Configuration Name: " + solutionConfiguration2.Name + 
            ", Platform Name: " + solutionConfiguration2.PlatformName);
      }

      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