다음을 통해 공유


How to: Programmatically Create Project Items

Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.

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

The GetProjectItemTemplate method returns the template from the appropriate .zip file for use with the AddFromTemplate method. The project item templates for all languages can be found in Program Files\Microsoft Visual Studio 10.0\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. In the left pane of the Options dialog box, click Projects and Solutions. Type the path of your templates in the Visual Studio user item templates location box.

Custom templates require unique file names that do not conflict with the file names that are defined in: Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\Language\.

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

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.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

Adding Items to Projects

To programmatically add items to a project

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

  2. Add the code that is shown later in this topic to the Connect class of the add-in.

  3. Run the add-in project and activate it in Add-In Manager by clicking Add-In Manager on the Tools menu and then selecting the box next to the add-in.

Example

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

Compiling the Code

To compile this code, create a Visual Studio add-in project and replace the code of the Connect.cs or Connect.vb class by using the code in the example. Before you run the add-in, open a Visual Basic project in Visual Studio. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

Robust Programming

When you use project item names as the parameter for Solution.Projects.Item, you must use the unique name of the project. The unique name is a relative path from the directory that contains 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").

See Also

Tasks

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

How to: Programmatically Create Projects

Concepts

Manipulating Visual Basic and Visual C# Projects

Manipulating Visual C++ Projects

Creating Templates for Projects and Items in Visual Studio

Other Resources

Controlling the Solution and Its Projects