Compartilhar via


Solution3.GetProjectTemplate Method

Returns a path to the specified project template.

If a template has a RequiredFrameworkVersion element higher than 4.0, you should provide the version in the call so that the search for the template will find a match. For example, instead of calling GetProjectTemplate("Extensibility\\1033\\VSIXProject.zip", "CSharp"); call GetProjectTemplate("Extensibility\\1033\\VSIXProject.zip|FrameworkVersion=4.5", "CSharp");.

Namespace:  EnvDTE90
Assembly:  EnvDTE90 (in EnvDTE90.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.

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

.NET Framework Security

See Also

Reference

Solution3 Interface

EnvDTE90 Namespace

Other Resources

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