Compartir a través de


Solution3.AddFromTemplate(String, String, String, Boolean) Method

Definition

Copies an existing project file, and any items or subdirectories it contains, to the specified location and adds it to the solution.

EnvDTE::Project AddFromTemplate(std::wstring const & FileName, std::wstring const & Destination, std::wstring const & ProjectName, bool Exclusive = false);
[System.Runtime.InteropServices.DispId(15)]
public EnvDTE.Project AddFromTemplate (string FileName, string Destination, string ProjectName, bool Exclusive = false);
[<System.Runtime.InteropServices.DispId(15)>]
abstract member AddFromTemplate : string * string * string * bool -> EnvDTE.Project
Public Function AddFromTemplate (FileName As String, Destination As String, ProjectName As String, Optional Exclusive As Boolean = false) As Project

Parameters

FileName
String

Required. The full path and file name with extension of the template project file.

Destination
String

Required. The full path of the directory in which to copy the contents of FileName.

ProjectName
String

Required. The name of the project file in the destination directory. This should include the extension. The displayed name is derived from ProjectName.

Exclusive
Boolean

Optional. Indicates whether the project loads in the current solution or its own; true if the current solution is closed and the project is added to a new solution, false if the project is added to the existing, open solution.

Returns

A Project object.

Implements

Attributes

Examples

Sub SolutionExample(ByVal dte As DTE2)  
    ' This function creates a solution and adds a Visual C# Console  
    ' project to it.  
    Try  
        Dim soln As Solution3 = CType(DTE.Solution, Solution3)  
        Dim csTemplatePath As String  
        ' This path must exist on your computer.  
        ' Replace <file path> below with an actual path.  
        Dim csPrjPath As String = "<file path>"  
        MsgBox("starting")  
        ' Get the project template path for a C# console project.  
        csTemplatePath = soln.GetProjectTemplate _  
        ("ConsoleApplication.zip", "CSharp")  
        ' Create a new C# Console project using   
        ' the template obtained above.  
        soln.AddFromTemplate(csTemplatePath, csPrjPath, _  
        "New CSharp Console Project", False)  
        MsgBox("done")  
    Catch ex As System.Exception  
        MsgBox(ex.ToString)  
    End Try  
End Sub  
public void SolutionExample(DTE2 dte)  
{  
    // This function creates a solution and adds a Visual C# Console  
    // project to it.  
    try{  
        Solution3 soln = (Solution3)_applicationObject.Solution;  
        String csTemplatePath;  
        // The file path must exist on your computer.  
        // Replace <file path> below with an actual path.  
        String csPrjPath = "<file path>";  
          "<file path>MessageBox.Show("Starting...");  
          "<file path>"<file path>csTemplatePath =   
          soln.GetProjectTemplate("ConsoleApplication.zip", "CSharp");  
        // Create a new C# Console project using the template obtained   
        // above.  
        soln.AddFromTemplate(csTemplatePath, csPrjPath,  
          "New CSharp Console Project", false);  
        MessageBox.Show("Done!");  
    }  
    catch(SystemException ex)  
    {  
        MessageBox.Show("ERROR: " + ex);  
    }  
}  

Remarks

The name of the project displayed in Solution Explorer is ProjectName without the file extension. AddFromTemplate fails if the new project file name already exists in the destination.

Note

For Visual Basic and Visual C# projects: The returned Project object has a value of Nothing or null. You can find the created Project object by iterating through the DTE.Solution.Projects collection by using the ProjectName parameter to identify the newly created project.

Applies to