Export (0) Print
Expand All

How to: Create Solution and Project Build Configurations

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see How to: Convert an Add-in to a VSPackage.

The Visual Studio automation model offers objects that enable you to control solution and project build configurations. A solution build configuration specifies how certain projects in a solution are to be built and, if enabled, deployed. Project build configurations, which are listed in the Project Property Pages dialog box, list all available types of project builds, such as Debug or Release, and the available platforms, such as .NET or Win32. For each combination of build and platform, there is a project configuration — a set of defined project properties and settings.

The solution build configuration objects are:

Object Name

Description

SolutionBuild2 object

Used to build, clean, and deploy the currently active solution configuration.

SolutionConfiguration2 object

Represents a list of projects and their configurations that are to be built.

SolutionConfigurations collection

Contains all defined SolutionConfiguration objects.

SolutionContext object

Represents a project's configuration in a SolutionConfiguration object.

SolutionContexts collection

Contains all SolutionContext objects in a SolutionConfiguration object.

BuildDependency object

Represents a project that must be built before the owning project can be built.

BuildDependencies collection

Contains all projects that must be built before the owning project can be built.

The project build configuration objects are:

Object Name

Description

ConfigurationManager object

Represents build configuration and platforms.

Configuration object

Represents a configuration, or set of build settings, within a specified platform.

Configurations collection

Contains all Configuration objects.

OutputGroup object

Contains the files that are built by the project.

OutputGroups collection

Contains all OutputGroup objects.

Using these objects you can:

  • Create, add projects to, activate, and delete solution configurations.

  • Build, run, or deploy any project in a solution configuration.

  • Obtain information about objects within a project or solution configuration.

  • Add, remove, or get information about project build dependencies.

Note Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Customizing Development Settings.

public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    SConfig(_applicationObject);
}
public void SConfig(DTE2 dte)
{
    try
    {
        SolutionBuild2 SB =
 (SolutionBuild2)_applicationObject.Solution.SolutionBuild;
        SolutionContext SolCtx;
        Project Proj;
        ConfigurationManager CM;
        SolutionConfigurations Cfgs;
        SolutionConfiguration2 Cfg;
        String msg;
        // Get a reference to the solution configurations
        // solution context of the first project.
        SolCtx = SB.SolutionConfigurations.Item(1).
SolutionContexts.Item(1);
        CM = dte.Solution.Projects.Item(1).ConfigurationManager;
        Proj = _applicationObject.Solution.Projects.Item(1);
        Cfgs = _applicationObject.Solution.
SolutionBuild.SolutionConfigurations;
        Cfg = (SolutionConfiguration2)Cfgs.Item(1);
        // List the current solution build info.
        msg = "BuildState = ";
        switch (SB.BuildState)
        {
            case vsBuildState.vsBuildStateNotStarted:
                msg = (msg + "Build has not yet started." + "\n");
            break;
            case vsBuildState.vsBuildStateInProgress:
                msg = (msg + "Build is in progress." + "\n");
            break;
            case vsBuildState.vsBuildStateDone:
                msg = (msg + "Build has completed." + "\n");
            break;
        }
        msg = msg + "Configuration Name = " + 
SolCtx.ConfigurationName + "\n";
        msg = msg + "Platform Name = " + SolCtx.PlatformName + "\n";
        msg = msg + "Project Name = " + SolCtx.ProjectName + "\n";
        MessageBox.Show(msg);
        // List the current solution configurations.
        msg = "Configuration names are:" + "\n";
        foreach(SolutionConfiguration2 tempConfigs in Cfgs)
        {
            msg = msg + tempConfigs.Name + "\n";
        }
        MessageBox.Show(msg);
        // Add a new solution configuration.
        Cfgs.Add("ANewConfiguration", "Debug", false);
        MessageBox.Show
("The name of the first solution configuration item is: " 
+ Cfgs.Item(1).Name);
        // Create a new project build configuration based on the 
        // Debug configuration.
        Proj.ConfigurationManager.AddConfigurationRow
("MyNewConfig", "Debug", true);
        // Build the debug solution configuration.
        MessageBox.Show("Build the solution in the debug mode...");
        SB.SolutionConfigurations.Item("Debug").Activate();
        SB.Build(true);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception: " + ex);
    }
}

To compile this code, create a new Visual Studio Add-in project and replace the code of the Connect.cs or Connect.vb class with the code in the example. Before running the add-in, open a project in the Visual Studio IDE. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

Show:
© 2014 Microsoft