Freigeben über


Gewusst wie: Programmgesteuertes Erstellen von Projektelementen

Aktualisiert: November 2007

To programmatically create project items, you first call GetProjectItemTemplate, and you then pass the returned template paths to AddFromTemplate. For more information, see Visual Studio-Vorlagen.

The GetProjectItemTemplate method returns the template from the appropriate .zip file for use with the AddFromTemplate method. The project item templates for all the languages can be found in <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\Language.

You can also create your own custom project item 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 item 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\ItemTemplates\Language.

Ensure that you use long file names (as opposed to 8dot3). For more information, see Erstellen von Projekt- und Elementvorlagen.

To remove projects from the solution, use Remove.

The following example addresses the generic method to create project items. Topics listed in the See Also section address how to use the language-specific models.

Hinweis:

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio-Einstellungen.

Adding Items to Projects

To programmatically add items to a project

  1. Start Visual Studio and create a new Visual Studio add-in project.

  2. Add the code below to the add-in's Connect class.

  3. Run the add-in project and activate it in the Add-In Manager.

    To do this, click Add-In Manager on the Tools menu and then check the box next to the add-in to activate it.

Beispiel

The following example demonstrates how to programmatically add items to an existing Visual Basic project.

' Before running the following code, be sure that a Visual Basic 
' project is open in Visual Studio.
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)
    createProjectItem(_applicationObject)
End Sub

Sub createProjectItem(ByVal dte As DTE2)
    ' Adds a new Class to an existing Visual Basic project.
    Dim soln As Solution2
    Dim prj As Project
    soln = CType(_applicationObject.Solution, Solution2)
    Dim prjItem As ProjectItem
    Dim itemPath As String

    ' Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1)
    ' Retrieve the path to the Class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj")
    ' Create a new project item based on the template, in this case,
    ' a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass")
End Sub
// Before running the following code, be sure that a Visual Basic 
// project is open in Visual Studio.
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.
    createProjectItem(_applicationObject);
}
public void createProjectItem(DTE2 dte)
{
    //Adds a new Class to an existing Visual Basic project.
    Solution2 soln;
    Project prj;
    soln = (Solution2)_applicationObject.Solution;
    ProjectItem prjItem;
    String itemPath;
    // Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1);
    // Retrieve the path to the class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj");
    //Create a new project item based on the template, in this
    // case, a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass");
}

Kompilieren des Codes

To compile this code, create a new Visual Studio Add-in project and replace the code of the Connect.cs or Connect.vb class with the code in the example. Before running the add-in, open a Visual Basic project in the Visual Studio IDE. For information about how to run an add-in, see Gewusst wie: Steuern von Add-Ins mit dem Add-In-Manager.

Robuste Programmierung

When using project item names as the parameter for Solution.Projects.Item, you must use the project's unique name. The unique name is a relative path from the directory containing the solution (.sln) file to the project file.

For example, consider following solution/project structure:

SomeSolution.sln

     WinApp1

          WinApp1.VBProj

The unique name for the project would be "WinApp1/WinApp1.VBProj" and the call to the Item method would be Solution.Projects.Item("WinApp1/WinApp1.VBProj").

Siehe auch

Aufgaben

Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell

Gewusst wie: Programmgesteuertes Erstellen von Projekten

Konzepte

Bearbeiten von Visual Basic- und Visual C#-Projekten

Bearbeiten von Visual C++-Projekten

Einführung in Visual Studio-Vorlagen

Weitere Ressourcen

Steuern der Projektmappe und der zugehörigen Projekte