How to: Migrate Code that Creates Projects by Using Templates

The process for creating projects has been updated in Visual Studio 2005 and the following procedure shows you how to update your existing applications that use automation to programmatically create code.

In Visual Studio .NET 2002 and Visual Studio .NET 2003, you created projects based on project templates by calling the AddFromTemplate method and passing in the name and path of the template file, such as .vsz, or .cpp, or .cs, and so forth.

In Visual Studio 2005, however, project template files are in compressed .zip files, so this process no longer works. This new project template system is described more fully in Creating Project Templates. The EnvDTE80 assembly provides new types to access these new compressed project templates. An updated version of AddFromTemplate as well as the new methods, GetProjectTemplate and GetProjectItemTemplate, enable you to programmatically create new projects and project items based on their compressed template files.

The process in Visual Studio 2005 is to use GetProjectTemplate to obtain the path to the specified template type, which you then pass to the AddFromTemplate method to create a new project. This allows you to avoid using hard-coded template paths. The following procedure illustrates how to do this.

To upgrade new project code

  1. Load, copy, or import the legacy code into Visual Studio.

  2. Replace the sections of the code that set project and project item template locations as hard-coded paths with the code (in the following section) that uses the GetProjectTemplate or GetProjectItemTemplate methods.

  3. Replace the AddFromTemplate method calls with the AddFromTemplate method.

  4. Compile and run the code.

Example

The following example demonstrates the procedure for programmatically creating projects and project items. It creates a Visual Basic console project and adds an HTML page (project item) to it.

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");
}

See Also

Tasks

How to: Compile and Run the Automation Object Model Code Examples

Concepts

Controlling Projects and Solutions

Introduction to the VSProject2 Object