HOW TO:以程式設計方式建立專案項目

若要以程式設計方式建立專案項目,首先要呼叫 GetProjectItemTemplate,然後將傳回的範本路徑傳遞至 AddFromTemplate。 如需詳細資訊,請參閱 Visual Studio 範本

GetProjectItemTemplate 方法會從適當的 .zip 檔傳回範本,以便與 AddFromTemplate 方法搭配使用。 所有語言的專案項目範本都可在下列位置找到:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\<語言>\。

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

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

請確定要使用長檔名 (不能使用 8dot3)。 如需詳細資訊,請參閱建立專案範本和項目範本

若要從方案移除專案,請使用 Remove

下列範例說明了建立專案項目的泛型方法。 <請參閱>一節中所列的主題會說明如何使用特定語言模型。

注意事項注意事項

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本和使用的設定決定了這些項目。 如需詳細資訊,請參閱 使用設定

將項目加入至專案

若要以程式設計方式將項目加入至專案

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

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

  3. 執行增益集專案,並按一下 [工具] 功能表上的 [增益集管理員],然後選取增益集旁邊的方塊,以便在 [增益集管理員] 中啟動該增益集。

範例

下列範例會示範如何以程式設計方式將項目加入至現有的 Visual Basic 專案。

' Before running the following code, be sure that a Visual Basic 
' project is open in 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)
    createProjectItem(_applicationObject)
End Sub

Sub createProjectItem(ByVal dte As DTE2)
    ' Adds a new Class to an existing Visual Basic project.
    Dim soln As Solution2
    Dim prj As Project
    soln = CType(_applicationObject.Solution, Solution2)
    Dim prjItem As ProjectItem
    Dim itemPath As String

    ' Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1)
    ' Retrieve the path to the Class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj")
    ' Create a new project item based on the template, in this case,
    ' a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass")
End Sub
// Before running the following code, be sure that a Visual Basic 
// project is open in Visual Studio.
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    // Pass the applicationObject member variable to the code example.
    createProjectItem(_applicationObject);
}
public void createProjectItem(DTE2 dte)
{
    //Adds a new Class to an existing Visual Basic project.
    Solution2 soln;
    Project prj;
    soln = (Solution2)_applicationObject.Solution;
    ProjectItem prjItem;
    String itemPath;
    // Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1);
    // Retrieve the path to the class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj");
    //Create a new project item based on the template, in this
    // case, a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass");
}

編譯程式碼

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

穩固程式設計

使用專案項目名稱做為 Solution.Projects.Item 的參數時,您必須使用專案的唯一名稱。 唯一名稱是從包含方案 (.sln) 檔至專案檔之目錄的相對路徑。

例如,考慮下列方案/專案結構:

SomeSolution.sln

     WinApp1

          WinApp1.VBProj

專案的唯一名稱會是「WinApp1/WinApp1.VBProj」,而對 Item 方法的呼叫會是 Solution.Projects.Item("WinApp1/WinApp1.VBProj")。

請參閱

工作

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

HOW TO:以程式設計方式建立專案

概念

管理 Visual Basic 和 Visual C# 專案

管理 Visual C++ 專案

Visual Studio 範本簡介

其他資源

控制方案及其專案