Partager via


Solution3.AddFromTemplate, méthode

Copie un fichier projet existant, ainsi que les éléments ou sous-répertoires qu'il contient, à l'emplacement spécifié, puis l'ajoute à la solution.

Espace de noms :  EnvDTE90
Assembly :  EnvDTE90 (dans EnvDTE90.dll)

Syntaxe

'Déclaration
Function AddFromTemplate ( _
    FileName As String, _
    Destination As String, _
    ProjectName As String, _
    Exclusive As Boolean _
) As Project
Project AddFromTemplate(
    string FileName,
    string Destination,
    string ProjectName,
    bool Exclusive
)
Project^ AddFromTemplate(
    [InAttribute] String^ FileName, 
    [InAttribute] String^ Destination, 
    [InAttribute] String^ ProjectName, 
    [InAttribute] bool Exclusive
)
abstract AddFromTemplate : 
        FileName:string * 
        Destination:string * 
        ProjectName:string * 
        Exclusive:bool -> Project
function AddFromTemplate(
    FileName : String, 
    Destination : String, 
    ProjectName : String, 
    Exclusive : boolean
) : Project

Paramètres

  • FileName
    Type : String

    Requis. Chemin d'accès complet et nom de fichier avec extension du fichier projet de modèle.

  • Destination
    Type : String

    Requis. Chemin d'accès complet du répertoire dans lequel vous voulez copier le contenu du fichier FileName.

  • ProjectName
    Type : String

    Requis. Nom du fichier projet tel qu'il apparaît dans le répertoire de destination. Il doit comprendre l'extension. Le nom affiché est dérivé de ProjectName.

  • Exclusive
    Type : Boolean

    Optionnel. Indique si le projet se charge dans la solution en cours ou dans sa propre solution ; true si la solution en cours est fermée et que le projet est ajouté à une nouvelle solution, false si le projet est ajouté à la solution ouverte existante.

Valeur de retour

Type : Project
Objet Project.

Notes

Le nom du projet affiché dans l'Explorateur de solutions est ProjectName sans l'extension de fichier. AddFromTemplate échoue si le nom du fichier du nouveau projet existe déjà dans la destination.

Notes

Pour les projets Visual Basic et Visual C# : l'objet Project retourné a la valeur Nothing ou nullune référence null (Nothing en Visual Basic).Pour rechercher l'objet Project créé, itérez au sein de la collection DTE.Solution.Projects à l'aide du paramètre ProjectName pour identifier le projet nouvellement créé.

Exemples

Pour plus d'informations sur l'exécution de ce code de complément, consultez Comment : compiler et exécuter les exemples de code du modèle objet Automation.

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

Sécurité .NET Framework

Voir aussi

Référence

Solution3 Interface

EnvDTE90, espace de noms

Autres ressources

Comment : compiler et exécuter les exemples de code du modèle objet Automation