Share via


Solution2.GetProjectTemplate Method

Returns a path to the indicated project template.

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

Syntax

'Declaration
Function GetProjectTemplate ( _
    TemplateName As String, _
    Language As String _
) As String
string GetProjectTemplate(
    string TemplateName,
    string Language
)
String^ GetProjectTemplate(
    String^ TemplateName, 
    String^ Language
)
abstract GetProjectTemplate : 
        TemplateName:string * 
        Language:string -> string 
function GetProjectTemplate(
    TemplateName : String, 
    Language : String
) : String

Parameters

  • Language
    Type: System.String
    The language used to write the template.

Return Value

Type: System.String
The full name of the project template.

Remarks

Project templates are stored as zip files. This method asks for the project by name and language and returns the path to the template.

The parameters of GetProjectTemplate can be supplied in a number of different ways as shown below:

  • Pass in "CSharp" as the Language parameter, and the name of the zip file as the TemplateName.

    GetProjectTemplate("PocketPC2003-ClassLibrary.zip", "CSharp");
    
  • Pass in "CSharp" as the Language parameter, and a partial file path;" PocketPC2003\ClassLibrary.vstemplate", to uniquely specify the TemplateName.

    GetProjectTemplate("PocketPC2003\ClassLibrary.vstemplate", "CSharp");//partial file path
    
  • Pass in the string "CSharp" as the Language parameter, and the string "Pocket PC 2003 Class Library" for the TemplateName parameter. The string "Pocket PC 2003 Class Library" is derived from the folder hierarchy and is referred to as the user interface (UI) string. Other examples of UI strings are "Console Application" and "Windows Application".

    Note

    The UI strings vary according to locale. Using the name of the zip file is the safest way to pass the TemplateName parameter.

    GetProjectTemplate("Pocket PC 2003 Class Library", "CSharp");
    
  • Pass in the string "CSharp" as the Language parameter and the string "PocketPC2003\Pocket PC 2003 Class Library" for the TemplateName parameter. This includes the UI string and a partial path to uniquely specify the template.

    GetProjectTemplate("PocketPC2003\Pocket PC 2003 Class Library", "CSharp");
    

You can also create your own custom project templates. To specify the directory in which you will store your templates, click Options on the Tools menu. On the left pane of the Options dialog box, click Projects and Solutions. Type the path for your templates in the Visual Studio user project templates location box. Alternatively, you can accept the default locations.

Custom templates require unique file names that do not conflict with the file names defined in:

  • <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language

Ensure that you use long file names (as opposed to 8dot3). For more information, see Creating Project and Item Templates.

Examples

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

The following example shows how to create a solution and add a console application project to it.

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)
    SolutionExample(_applicationObject)
End Sub

Sub SolutionExample(ByVal dte As DTE2)
    ' This function creates a solution and adds a Visual Basic Console
    ' project to it.
    Try
        Dim soln As Solution2 = CType(DTE.Solution, Solution2)
        Dim vbTemplatePath As String
        ' This path must exist on your computer.
        ' Replace <file path> below with an actual path.
        Dim vbPrjPath As String = "<file path>"
        MsgBox("starting")
        ' Get the project template path for a Visual Basic console project.
        vbTemplatePath = soln.GetProjectTemplate _
        ("ConsoleApplication.zip", "VisualBasic")
        ' Create a new Visual Basic Console project using the template obtained 
        ' above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
        "New Visual Basic Console Project", False)
        MsgBox("done")
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub
//make sure to add this reference to your project references
using System.Windows.Forms;
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.
    SolutionExample((DTE2)_applicationObject);
}

public void SolutionExample(DTE2 dte)
{
    // This function creates a solution and adds a Visual C# Console
    // project to it.
    try{
        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("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);
    }
}

.NET Framework Security

See Also

Reference

Solution2 Interface

EnvDTE80 Namespace