HOW TO:移轉使用範本建立專案的程式碼
更新:2007 年 11 月
建立專案的處理序已在 Visual Studio 2005 中更新,而下列程序會示範如何更新使用 Automation 以程式設計方式建立程式碼的現有應用程式。
在 Visual Studio .NET 2002 和 Visual Studio .NET 2003 中,您透過呼叫 AddFromTemplate 方法,並傳入範本檔案的名稱和路徑 (例如 .vsz 或 .cpp 或 .cs 等等),便能依據專案範本建立專案。
然而在 Visual Studio 2005 中,專案範本檔案是位於壓縮的 .zip 檔,所以這個程序無法再運作。這個新專案範本系統在建立專案範本中會有更詳細的描述。EnvDTE80 組件提供新類型可存取這些新的壓縮專案範本。AddFromTemplate 的更新版本和新方法 (GetProjectTemplate 和 GetProjectItemTemplate) 能讓您依據壓縮的範本專案,以程式設計方式建立新專案和專案項目。
Visual Studio 2005 中的處理序是使用 GetProjectTemplate,以取得指定的範本類型的路徑,然後您可將此路徑傳遞至 AddFromTemplate 方法以建立新專案。這能讓您避免使用硬式編碼的範本路徑。下列程序會說明如何執行此作業。
若要升級新的專案程式碼
載入、複製或匯入舊版程式碼至 Visual Studio。
以使用 GetProjectTemplate 或 GetProjectItemTemplate 方法的程式碼 (位於下列章節中) 取代將專案和專案項目範本位置設定為硬式編碼路徑的程式碼區段。
以 AddFromTemplate 方法取代 AddFromTemplate 方法呼叫。
編譯並執行程式碼。
範例
下列範例示範以程式設計方式建立專案和專案項目的程序。它會建立 Visual Basic 主控台專案,並將 HTML 網頁 (專案項目) 加入其中。
Sub CreatePrjAndPrjItemExample()
' Get a reference to the Solution2 object and create
' the path variables.
Dim soln As Solution2 = CType(DTE.Solution, Solution2)
Dim vbPrjTemplatePath As String
Dim vbItemTemplatePath As String
Dim vbPrjPath As String = "C:\MyNewVBProject"
Dim prjName As String = "New Visual Basic Console Project"
Dim prj As Project
Dim prjItems As ProjectItems
MsgBox("Starting...")
' Get the project template path for a Visual Basic console project.
vbPrjTemplatePath = _
soln.GetProjectTemplate("ConsoleApplication.zip", _
"VisualBasic")
' Create a new Visual Basic Console project by using the
' template obtained above.
soln.AddFromTemplate(vbPrjTemplatePath, vbPrjPath, prjName, False)
MsgBox("Done.")
' Reference the project and its items.
prj = soln.Projects.Item(1)
prjItems = prj.ProjectItems
' Get the path to the HTML Page template and add it to the project.
vbItemTemplatePath = soln.GetProjectItemTemplate("HTMLPage.zip", _
"VisualBasic")
prjItems.AddFromTemplate(vbItemTemplatePath, "A New HTML Page")
End Sub
public void CreatePrjAndPrjItemExample(DTE2 dte)
{
// Before running, set a reference to
// System.Windows.Forms.
// =============================
// Get a reference to the Solution2 object and create
// the path variables.
Solution2 soln = (Solution2) dte.Solution;
string vbPrjTemplatePath;
string vbItemTemplatePath;
string vbPrjPath = "C:\\MyNewVBProject";
string prjName = "New Visual Basic Console Project";
Project prj;
ProjectItems prjItems;
System.Windows.Forms.MessageBox.Show("Starting...");
// Get the project template path for a Visual Basic console project.
vbPrjTemplatePath = soln.GetProjectTemplate _
("ConsoleApplication.zip", VisualBasic");
// Create a new Visual Basic Console project by using
// the template obtained above.
soln.AddFromTemplate(vbPrjTemplatePath, vbPrjPath, prjName, false);
System.Windows.Forms.MessageBox.Show("Done.");
// Reference the project and its items.
prj = soln.Projects.Item(1);
prjItems = prj.ProjectItems;
// Get the path to the HTML Page template and add it to the
// project.
vbItemTemplatePath = soln.GetProjectItemTemplate _
("HTMLPage.zip", "VisualBasic");
prjItems.AddFromTemplate(vbItemTemplatePath, "A New HTML Page");
}
請參閱
工作
HOW TO:編譯和執行 Automation 物件模型程式碼範例