SolutionFolder.AddFromTemplate 方法
依據專案範本將新專案加入至方案資料夾。
命名空間: EnvDTE80
組件: EnvDTE80 (在 EnvDTE80.dll 中)
語法
'宣告
Function AddFromTemplate ( _
FileName As String, _
Destination As String, _
ProjectName As String _
) As Project
Project AddFromTemplate(
string FileName,
string Destination,
string ProjectName
)
Project^ AddFromTemplate(
[InAttribute] String^ FileName,
[InAttribute] String^ Destination,
[InAttribute] String^ ProjectName
)
abstract AddFromTemplate :
FileName:string *
Destination:string *
ProjectName:string -> Project
function AddFromTemplate(
FileName : String,
Destination : String,
ProjectName : String
) : Project
參數
- FileName
型別:System.String
專案範本的完整路徑。
- Destination
型別:System.String
這是要在其中複製 FileName 內容的目錄完整路徑。
- ProjectName
型別:System.String
要建立的新專案名稱。
傳回值
型別:EnvDTE.Project
Project 物件。
備註
如果目的端中已存在新專案的檔名,這個呼叫將會失敗。
範例
這個範例中,會建立新方案資料夾,並從現有檔案中加入專案至此資料夾。 它也會從範本中加入專案。 在執行這個範例之前,請先在主磁碟機 (在範例中為 "C:") 以外的地方建立一個 "Projects" 資料夾,並在其中建立名為 "ClassLibrary1" 的 Visual C# 類別庫專案。 您也必須在 [Projects] 資料夾中建立名為 "MyCSProject" 的資料夾。 執行這個增益集之前,先開啟 Visual Studio 整合式開發環境 (IDE) 中的專案。
如需如何像執行增益集一般,執行這個範例的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例。
Imports EnvDTE
Imports EnvDTE80
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)
solnFolderAddFromTemplateExample(_applicationObject)
End Sub
Sub solnFolderAddFromTemplateExample(ByVal dte As DTE2)
' Before running this example, create a "Projects" folder
' off your main drive ("C:" in this example), and create a C#
' class library project, named ClassLibrary1 in that folder.
Dim soln As Solution2 = CType(_applicationObject.Solution _
, Solution2)
Dim prj As Project
Dim sb As New System.Text.StringBuilder
Dim SF As SolutionFolder
Try
Dim prjPath As String = _
"C:\Projects\ClassLibrary1\ClassLibrary1\ClassLibrary1.csproj"
' Open a project in the Visual Studio IDE before
' running this add-in.
' Add a solution folder.
prj = soln.AddSolutionFolder("A new soln folder")
SF = CType(prj.Object, SolutionFolder)
' Add a project to the new solution folder.
SF.AddFromFile(prjPath)
MsgBox("Added a new solution folder that contains a _
C# project named ClassLibrary1.")
' Get the project template path for a C# console project.
Dim csTemplatePath As String = soln.GetProjectTemplate _
("Console Application", "CSharp")
' Before running this example, create a
' "Projects\MyCSProject" folder
' off your main drive ("C:" in this example).
Dim prjPath2 As String = "C:\Projects\MyCSProject"
' Add a new C# Console project to the solution folder
' by using the template obtained above.
SF.AddFromTemplate(csTemplatePath, prjPath2, _
"New CSharp Console Project")
MsgBox("Added a new project from template _
to the solution folder.")
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
solnFolderAddFromTemplateExample(_applicationObject);
}
public void solnFolderAddFromTemplateExample(DTE2 dte)
{
// Before running this example, create a "Projects" folder
// off your main drive (C: in this example), and create a C#
// class library project, named ClassLibrary1 in that folder.
Solution2 soln = (Solution2)_applicationObject.Solution;
Project prj;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
SolutionFolder SF;
try
{
String prjPath = "C:\\Projects\\ClassLibrary1
\\ClassLibrary1\\ClassLibrary1.csproj";
// Open a project in the Visual Studio IDE before
// running this add-in.
// Add a solution folder.
prj = soln.AddSolutionFolder("A new soln folder");
SF = (SolutionFolder)prj.Object;
// Add a project to the new solution folder.
SF.AddFromFile(prjPath);
MessageBox.Show("Added a new solution folder that contains a
C# project named ClassLibrary1.");
// Get the project template path for a C# console project.
String csTemplatePath = soln.GetProjectTemplate
("Console Application", "CSharp");
// Before running this example, create
// a "Projects\MyCSProject" folder
// off your main drive ("C:" in this example).
String prjPath2 = "C:\\Projects\\MyCSProject";
// Add a new C# Console project to the solution folder
// by using the template obtained above.
SF.AddFromTemplate(csTemplatePath, prjPath2,
"New CSharp Console Project");
MessageBox.Show("Added a new project from
template to the solution folder.");
}
catch(SystemException ex)
{
MessageBox.Show(ex.ToString());
}
}
.NET Framework 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。