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

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

This article shows how to change from a Visual Studio add-in the build platform of a project for a given a build solution configuration.

More Information

To change the platform of a project you need to assign the EnvDTE.SolutionContext.ConfigurationName property (which is writable) to a value in the form "configuration|platform" 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 ConfigurationName property if you want to locate the context for a specific configuration.

Note: notice that the EnvDTE.SolutionContext.PlatformName is read-only, so you have to use this approach "configuration|platform" introduced in Visual Studio 2012 (previous versions don't support it, see PRB: Cannot change the build platform of a project from a Visual Studio add-in).

The following add-in, when loaded, adds a project platform "x86" and changes the platform of all the contexts of all the solution configurations to that value 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();

         project.ConfigurationManager.AddPlatform("x86", "Any CPU", false);

         ChangeProjectContexts(project, "x86");

         ShowProjectContexts();
      }

      private void ChangeProjectContexts(EnvDTE.Project project, string platformName)
      {
         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 = solutionContext.ConfigurationName + "|" + platformName;
               }
            }
         }
      }

      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 to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top