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

参数

  • Destination
    类型:System.String
    这是复制 FileName 内容的目录的完整路径。
  • ProjectName
    类型:System.String
    要创建的新项目的名称。

返回值

类型:EnvDTE.Project
一个 Project 对象。

备注

若目标中已存在新项目文件名,则此调用失败。

示例

此示例创建一个新解决方案文件夹并从现有的文件向该文件夹中添加一个项目。 它还从模板添加项目。 在运行此示例之前,在主驱动器(在此示例中为“C:”)中创建一个“Projects”文件夹,然后在该文件夹中创建一个名为“ClassLibrary1”的 Visual C# 类库项目。 还必须在“Projects”文件夹中创建一个名为“MyCSProject”的文件夹。 在运行此外接程序之前,在 Visual Studio 集成开发环境 (IDE) 中打开项目。

有关如何作为外接程序运行此示例的更多信息,请参见 如何:编译和运行自动化对象模型代码示例

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 安全性

请参见

参考

SolutionFolder 接口

EnvDTE80 命名空间