Export (0) Print
Expand All

ItemOperations.AddNewItem Method

Adds a new item to the current project.

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

ProjectItem AddNewItem(
	string Item,
	string Name
)

Parameters

Item
Type: System.String

Optional. The virtual path to the item based on either the UI resource string (defined in the Remarks section below) or occasionally as displayed in the contents of the New File dialog box in "category\templatename" format. Category comes from the left pane of the dialog box and templatename from the right pane. For example, to add a new HTML file to a Visual Basic project, you would use "General\HTML Page." See the Remarks section for more information.

Name
Type: System.String

Optional. The display name to use for the item when it is added to a project.

Return Value

Type: EnvDTE.ProjectItem
A ProjectItem object.

AddNewItem models the New File dialog box. The contents of this dialog box vary depending on the type of project. There are two ways to determine the values to place in the Item parameter: look up the values in the New File dialog box, or look up the values in the appropriate UI resource file.

To add items to a some projects by using the first method, look at the items available for the project in the New File dialog box. The New File dialog box has two panes: Categories and Templates. The Item parameter of the AddNewItem method is

in "category\templatename" format. Category usually comes from the left pane of the dialog box (Categories) and templatename from the right pane (Templates). For example, to add a new HTML file to a Visual Basic project, you would use "General\HTML Page." Because of language differences, however, the exact values for this are not always reflected precisely in the New File dialog box.

A more consistent and reliable method for getting the exact values for the Item parameter is to look up the necessary string in the UI resource file for each programming language. You then use this value along with the name of the item you want to add, as it displays in the New File and Add New Item dialog boxes. For example, to add a new Visual Basic class file to your project, you would use AddNewItem("Visual Basic Items\Class", "test.vb"). To add a new Visual C# class file to your project, you would use AddNewItem("Visual C# Project Items\Class", test.cs");.

The string "Visual Basic Items" (as well as other language items) is found the following way:

  • For the Visual Basic file, open the following registry key: HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\8.0\Projects\{F184B08F-C81C-45f6-A57F-5ABD9991F28F}\AdditemTemplates\TemplateDirs\{164B10B9-B200-11D0-8C61-00A0C91E29D5}\ /1. Its default value is #3082.

  • Open the file msvbprjui.dll in Visual Studio using the Resource editor. To do this, browse to the file by selecting File… on the Open menu, which is on the File menu, and then open the file. Open the String Table node and look at resource ID 3082, it is set to "Visual Basic Items." This is the string you put in the AddNewItem call. The same procedure applies to the Visual C# , Visual C++, and Visual C# project systems except the file names and resource IDs to use are different.

Following is a table of resource IDs and locations for the UI resource files:

Programming Language

UI File Name

Resource ID for Class

Visual Basic

…\Program Files\Microsoft Visual Studio 8\VB\Bin\1033\msvbprojUI.dll

3082

Visual C#

…\Program Files\Microsoft Visual Studio 8\VC#\VCSPackages\1033\csprjui.dll

2346

Visual C++

…\Program Files\Microsoft Visual Studio 8\VC\vcpackages\1033\VCProjectUI.dll

10007

Sub AddNewItemExample()
   Dim ItemOp As ItemOperations
   ItemOp = DTE.ItemOperations
   ItemOp.AddNewItem("General\Text File", "A new text file")
End Sub

Show:
© 2014 Microsoft