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. |
To programmatically add items to a project
-
Start Visual Studio and create a Visual Studio add-in project.
-
Add the code that is shown later in this topic to the Connect class of the add-in.
-
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.
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.
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").
Note