HOW TO:建立方案和專案組建組態

更新:2007 年 11 月

Visual Studio Automation 模型會提供能讓您控制方案與專案建置組態的物件。方案組建組態會指定方案中特定專案的建置和 (如果啟用的話) 部署方式。專案組建組態會列在 [專案屬性頁] 對話方塊中,並且會列出所有可用的專案組建類型 (例如偵錯或發行) 及可用的平台 (例如 .NET 或 Win32)。每一種組建和平台的組合都有一個專案組態,也就是一組已定義的專案屬性和設定。

方案組建組態物件有:

物件名稱

描述

SolutionBuild2 物件

用以建置、清除,及部署目前使用中的方案組態。

SolutionConfiguration2 物件

表示專案及要建置之組態的清單。

SolutionConfigurations 集合

包含所有定義的 SolutionConfiguration 物件。

SolutionContext 物件

表示 SolutionConfiguration 物件中的專案組態。

SolutionContexts 集合

包含 SolutionConfiguration 物件中的所有 SolutionContext 物件。

BuildDependency 物件

表示必須在建置主控專案之前建置的專案。

BuildDependencies 集合

包含必須在建置主控專案之前建置的所有專案。

專案組建組態的物件如下:

物件名稱

描述

ConfigurationManager 物件

表示組建組態與平台。

Configuration 物件

表示指定平台中的一個組態或一組組建設定。

Configurations 集合

包含所有 Configuration 物件。

OutputGroup 物件

包含由專案建置的檔案。

OutputGroups 集合

包含所有 OutputGroup 物件。

使用這些物件,您可以:

  • 建立、加入專案至、啟動及刪除方案組態。

  • 在方案組態中建置、執行或部署任何專案。

  • 取得專案或方案組態中物件的相關資訊。

  • 加入、移除或取得專案組件相依性的相關資訊。

注意事項:

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定

範例

Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    SConfig(_applicationObject)
End Sub
Sub SConfig(ByVal dte As DTE2)
    Dim SB As SolutionBuild2 =  _
    CType(_applicationObject.Solution.SolutionBuild, _
    SolutionBuild2)
    Dim SolCtx As SolutionContext
    Dim Proj As Project
    Dim CM As ConfigurationManager
    Dim Cfgs As SolutionConfigurations
    Dim Cfg As SolutionConfiguration2
    Dim msg As String

    ' Get a reference to the solution configurations 
    ' solution context of the first project.
    SolCtx = SB.SolutionConfigurations.Item(1).SolutionContexts.Item(1)
   CM = _applicationObject.Solution.Projects. _
    Item(1).ConfigurationManager
    Proj = _applicationObject.Solution.Projects.Item(1)
    Cfgs = _applicationObject.Solution.SolutionBuild. _
    SolutionConfigurations
    Cfg = CType(Cfgs.Item(1), SolutionConfiguration2)

    ' List the current solution build info.
    msg = "BuildState = "
    Select Case SB.BuildState
        Case vsBuildState.vsBuildStateNotStarted
            msg = msg & "Build has not yet started." & vbCr
        Case vsBuildState.vsBuildStateInProgress
            msg = msg & "Build is in progress." & vbCr
        Case vsBuildState.vsBuildStateDone
            msg = msg & "Build has completed." & vbCr
    End Select
    msg = msg & "Configuration Name = " & SolCtx.ConfigurationName _
    & vbCr
    msg = msg & "Platform Name = " & SolCtx.PlatformName & vbCr
    msg = msg & "Project Name = " & SolCtx.ProjectName & vbCr
    MsgBox(msg)

    ' List the current solution configurations.
    msg = ("Configuration names are:" & vbCr)
    For Each Cfg In Cfgs
        msg = msg & Cfg.Name & vbCr
    Next
    MsgBox(msg)
   ' Add a new solution configuration.
   Cfgs.Add("ANewConfiguration", "Debug", False)
   MsgBox(Cfgs.Item(1).Name)
   ' Create a new project build configuration based on the Debug 
   ' configuration.
   Proj.ConfigurationManager.AddConfigurationRow("MyNewConfig", _
    "Debug", True)
   ' Build the solution configuration.
   sb.SolutionConfigurations.Item("MyConfig").Activate()
   sb.Build()
End Sub
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);
    }
}

編譯程式碼

若要編譯這個程式碼,請建立新的 Visual Studio 增益集專案,並以範例中的程式碼取代 Connect.cs 或 Connect.vb 類別的程式碼。執行增益集之前,請在 Visual Studio IDE 中開啟專案。如需如何執行增益集的詳細資訊,請參閱 HOW TO:以增益集管理員控制增益集

請參閱

工作

HOW TO:新增和處理命令

HOW TO:建立增益集

逐步解說:建立精靈

概念

方案、專案和項目簡介

Automation 物件模型圖表

其他資源

在 Visual Studio 中建置

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考