共用方式為


如何:以程式設計方式建立專案

.Visual Studio 增益集在 Visual Studio 2013 中已不適用。 您應該升級您的增益集至 VSPackage 擴充套件。 如需升級的詳細資訊,請參閱 常見問題集:將增益集轉換成 VSPackage 擴充功能

若要建立專案,請呼叫 GetProjectTemplate,然後將傳回的範本路徑傳遞至 AddFromTemplate

專案範本具有 .vstemplate 副檔名,而且儲存在 .zip 檔中。 若要取得 .vstemplate 檔 (在 .zip 檔中) 的路徑,請使用 GetProjectTemplate,然後將其傳遞至 AddFromTemplate 以建立專案 (如果尚未開啟任何方案,也會建立方案)。 您可以按照需求多次執行此作業,且每個專案都會加入至目前開啟的方案。

所有語言的專案範本都可在此處找到:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\Language\。

您也可以建立自己的自訂專案範本。 若要指定用來儲存範本的目錄,請按一下 [工具] 功能表上的 [選項], 然後在 [選項] 對話方塊的左窗格中,按一下 [專案和方案], 在 [Visual Studio 使用者專案範本位置] 方塊中輸入範本的路徑。

自訂專案範本需要唯一的檔案名稱,而且此名稱不會與下列位置中所定義的檔名相衝突:Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\Language\。

請確定要使用長檔名 (不能使用 8dot3)。 如需詳細資訊,請參閱Creating Project and Item Templates

注意事項注意事項

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

建立專案

若要以程式設計方式建立專案

  1. 啟動 Visual Studio 並建立 Visual Studio 增益集專案。

  2. 將本主題稍後會示範的範例程式碼加入至增益集 Connect 類別。

  3. 執行增益集專案,並在 [增益集管理員] 中啟動它。

    若要這麼做,請按一下 [工具] 功能表上的 [增益集管理員],然後選取增益集。

範例

下列範例使用 GetProjectTemplateAddFromTemplate 在方案中建立兩個主控台專案:其中一個是 Visual Basic,另一個則是 Visual C#。

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)
    createProjectsFromTemplates(_applicationObject)
End Sub

Sub createProjectsFromTemplates(ByVal dte As DTE2)
    Try
        ' Create a solution with two projects in it, based on project 
        ' templates.
        Dim soln As Solution2 = CType(DTE.Solution, _
        Solution2)
        Dim csTemplatePath As String
        Dim vbTemplatePath As String
        Dim csPrjPath As String = _
        "C:\UserFiles\user1\addins\MyCSProject"
        Dim vbPrjPath As String = _
        "C:\UserFiles\user1\addins\MyVBProject"

        ' Get the project template path for a C# console project.
        ' Console Application is the template name that appears in the 
        ' right pane, "CSharp" is the Language(vstemplate) as seen in 
        ' the registry.
        csTemplatePath = soln.GetProjectTemplate _
        ("ConsoleApplication.zip", "CSharp")
        MsgBox("C# template path: " & csTemplatePath)
        ' Get the project template path for a Visual Basic
        ' console project.
        ' "vbproj: is the DefaultProjectExtension as seen in the 
        ' registry.
        vbTemplatePath = soln.GetProjectTemplate _
        ("ConsoleApplication.zip", "vbproj")
        MsgBox("Visual Basic template path: " & vbTemplatePath)
        ' Create a new C# console project using the template obtained 
        ' above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath, _
          "New CSharp Console Project", False)
        ' Create a new Visual Basic console project using the template
        ' obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
          "New Visual Basic Console Project", False)
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.ToString)
    End Try
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    createProjectsFromTemplates(_applicationObject);
}

public void createProjectsFromTemplates(DTE2 dte)
{
    try
    {
        // Create a solution with two projects in it, based on project 
        // templates.
        Solution2 soln = (Solution2)dte.Solution;
        string csTemplatePath;
        string vbTemplatePath;
        string csPrjPath = "C:\\UserFiles\\user1\\addins\\MyCSProject";
        string vbPrjPath = "C:\\UserFiles\\user1\\addins\\MyVBProject";
        // Get the project template path for a C# console project.
        // Console Application is the template name that appears in 
        // the right pane. "CSharp" is the Language(vstemplate) as seen 
        // in the registry.
        csTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip", 
          "CSharp");
        System.Windows.Forms.MessageBox.Show("C# template path: " + 
          csTemplatePath);
        // Get the project template path for a Visual Basic console
        // project.
        // "vbproj: is the DefaultProjectExtension as seen in the 
        // registry.
        vbTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip", 
          "vbproj");
        System.Windows.Forms.MessageBox.Show("Visual Basic template path: " + 
          vbTemplatePath);
        // Create a new C# console project using the template obtained 
        // above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath, "New CSharp 
          Console Project", false);
        // Create a new Visual Basic console project using the template 
        // obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "New VB Console 
          Project", false);
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
    }
}

請參閱

工作

如何:編譯和執行 Automation 物件模型程式碼範例

如何:以程式設計方式建立專案項目

概念

管理 Visual Basic 和 Visual C# 專案

管理 Visual C++ 專案

其他資源

控制方案及其專案