HOWTO: Performing some action before or after a build from a Visual Studio macro or add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: June 2007   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012


This article explains how to perform some action before or after a build.

More Information

There are several ways to perform some automated action before or after a solution or project is built, or to automate a build or retrieve build configuration:
  • You can use the pre-build and post-build events that the Visual Studio IDE provides for each project. These events allow you to set a command line to run an executable, which can receive some parameters (macro variables such as project name, etc). You can set the command line for these events in the following locations:

    C# Projects:

    • Visual Studio 2005 and higher: Project properties window, Build Events tab.
    • Visual Studio .NET 2003: Project Properties window, Common Properties section, Build Events subsection.
    • Visual Studio .NET 2002: N/A

    VB.NET Projects:

    • Visual Studio 2005 and higher: Project properties window, Compile tab, Build Events button
    • Visual Studio .NET 2003: N/A
    • Visual Studio .NET 2002: N/A

    C++ Projects:

    • Any Visual Studio version: Project Properties window, Configuration Properties section, Build Events subsection.

  • You can use the EnvDTE.BuildEvents class of the automation model. You can retrieve an instance of this class through the EnvDTE.DTE.Events.BuildEvents property. This class provides the following events:
    • OnBuildBegin / OnBuildDone, to receive events before or after a build is performed. You receive as parameters the scope (solution, project) and the action (build, rebuild, clean, etc.)
    • OnBuildProjConfigBegin / OnBuildProjConfigDone, to receive events before or after a project configuration is built. You receive as parameters the project unique name, which is a String (if you need to get the EnvDTE.Project object from this project unique name see the article HOWTO: Get an EnvDTE.Project object from its project unique name), the project configuration (typically "Debug" or "Release"), the platform and the solution configuration.

      You can get information about the solution configuration using the DTE.Solution.SolutionBuild property, which returns an EnvDTE.SolutionBuild class that has the properties SolutionConfigurations (and each SolutionConfiguration has a SolutionContexts property, with a SolutionContext for each project and parameters like ShouldBuild, ShouldDeploy, etc.), ActiveConfiguration, BuildDependencies, etc. along with methods to build programmatically such as Build and BuildProject.

      You can also get and manipulate the Configuration Manager programmatically using the EnvDTE.Project.ConfigurationManager property. The EnvDTE.ConfigurationManager class allows to retrieve the platform names, the supported platforms, the configuration rows, etc.

      If you are using a macro and not an add-in, see how to set event handlers in the article HOWTO: Initializing new events in Visual Studio macros.
  • You can capture the several "Build.XXX" command events as described in the article HOWTO: Capturing commands events from Visual Studio .NET add-ins.
  • You can use the IVsBuildXXX interfaces of the Visual Studio SDK. If you are using an add-in and not an SDK package, see the article HOWTO: Get a Visual Studio service from an add-in.
  • When performing builds from the command line (devenv.exe /Build), your add-in can be loaded if its LoadBehavior setting includes the value 4. In that case the connectMode of the OnConnection method receives the value  ext_cm_CommandLine.
  • To cancel a build programmatically see the article HOWTO: Canceling a build from a Visual Studio .NET add-in.

Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)