This documentation is archived and is not being maintained.

Solution2.AddFromTemplate Method 

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

Namespace: EnvDTE80
Assembly: EnvDTE80 (in envdte80.dll)

Project AddFromTemplate (
	[InAttribute] string FileName,
	[InAttribute] string Destination,
	[InAttribute] string ProjectName,
	[OptionalAttribute] [InAttribute] bool Exclusive
Project AddFromTemplate (
	/** @attribute InAttribute() */ String FileName, 
	/** @attribute InAttribute() */ String Destination, 
	/** @attribute InAttribute() */ String ProjectName, 
	/** @attribute InAttribute() */ /** @attribute OptionalAttribute() */ boolean Exclusive
function AddFromTemplate (
	FileName : String, 
	Destination : String, 
	ProjectName : String, 
	Exclusive : boolean
) : Project



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


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


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


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.

Return Value

A Project object.

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.


For Visual Basic and Visual C# projects: The returned Project object has a value of Nothing or a null reference (Nothing in Visual Basic). 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.

For information on how to run this add-in code, see How to: Compile and Run the Automation Object Model Code Examples.

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.

public void SolutionExample(DTE2 dte)
    // This function creates a solution and adds a Visual C# Console
    // project to it.
        Solution2 soln = (Solution2)_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("", "CSharp");
        // Create a new C# Console project using the template obtained 
        // above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath,
 "New CSharp Console Project", false);
    catch(SystemException ex)
        MessageBox.Show("ERROR: " + ex);