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

More Information

To add a build project platform you have to use the AddPlatform method of the EnvDTE.ConfigurationManager type (that you can get from the EnvDTE.Project.ConfigurationManager property). This method receives as parameters:

  • The new platform name
  • The name of an existing platform to copy its settings to the new configuration.
  • A boolean parameter to specify whether to create new build solution platform or not.

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

The following add-in, when loaded, adds and deletes build project platforms 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);

         ShowPlatforms(project);

         AddProjectPlatform(project, "x86", "Any CPU", false);

         ShowPlatforms(project);

         DeleteProjectPlatform(project, "x86");

         AddProjectPlatform(project, "x64", "Any CPU", true);

         ShowPlatforms(project);
      }

      private void AddProjectPlatform(EnvDTE.Project project, string newPlatformName, string existingPlatformName, bool propagateToSolution)
      {
         project.ConfigurationManager.AddPlatform(newPlatformName, existingPlatformName, propagateToSolution);
      }

      private void DeleteProjectPlatform(EnvDTE.Project project, string platformName)
      {
         project.ConfigurationManager.DeletePlatform(platformName);
      }

      private void ShowPlatforms(EnvDTE.Project project)
      {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         EnvDTE80.SolutionBuild2 solutionBuild2;
         List<string> usedNames = new List<string>();
         string solutionPlatformName;

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

         // Solution platform names
         sb.AppendLine();
         sb.AppendLine("-----------------------------------------------");
         sb.AppendLine("Solution platform names:");
         sb.AppendLine();
         usedNames.Clear();

         foreach (SolutionConfiguration2 solutionConfiguration2 in solutionBuild2.SolutionConfigurations)
         {
            solutionPlatformName = solutionConfiguration2.PlatformName;

            if (!usedNames.Contains(solutionPlatformName))
            {
               usedNames.Add(solutionPlatformName);
               sb.AppendLine("   - " + solutionPlatformName);
            }
         }

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

         foreach (object projectPlatformName in (object[]) project.ConfigurationManager.PlatformNames)
         {
            sb.AppendLine("   - " + projectPlatformName.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