Visual Studio Automation and Extensibility Reference
ProjectItems..::.AddFromFile Method

Adds a project item from a file that is installed in a project directory structure.

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

Visual Basic (Declaration)
Function AddFromFile ( _
    FileName As String _
) As ProjectItem
Visual Basic (Usage)
Dim instance As ProjectItems
Dim FileName As String
Dim returnValue As ProjectItem

returnValue = instance.AddFromFile(FileName)
C#
ProjectItem AddFromFile(
    string FileName
)
Visual C++
ProjectItem^ AddFromFile(
    [InAttribute] String^ FileName
)
JScript
function AddFromFile(
    FileName : String
) : ProjectItem

Parameters

FileName
Type: System..::.String
Required. The file name of the item to add as a project item.

Return Value

Type: EnvDTE..::.ProjectItem
A ProjectItem object.
Remarks

AddFromFile fails if the file to be added is already in the project.

FileName need not contain a full path as long as the file can be found. How a file is found depends on the programming language. In order to use pathless files in Visual Basic and Visual C#, for example, the files must be in the same directory as the project. If they are not, they are copied over. Visual C++ allows linking to files anywhere on the system.

Examples

This example works only in Visual Studio .NET 2003. For more information, see How to: Migrate Code that Creates Projects by Using Templates.

Visual Basic
Sub AddFromFileExample()
   ' This function creates a solution and adds a Visual Basic Console
   ' project to it. 
   Dim soln As Solution
   Dim proj As Project
   Dim projitems As ProjectItems

   ' Create a reference to the solution.
   soln = DTE.Solution

   ' Create a new solution.
   soln.Create("c:\temp2", "MyNewSolution")

   ' Create a new Visual Basic Console application project.
   ' Adjust the save path as needed.
   proj = soln.AddFromTemplate("C:\Program Files\Microsoft Visual Studio .NET\Vb7\VBWizards\ConsoleApplication\Templates\1033\ConsoleApplication.vbproj", "c:\temp2", "My New Project", True)
   projitems = proj.ProjectItems

   ' Add a project item from a file.
   projitems.AddFromFile("C:\temp\An Item.bas")
End Sub
Permissions

See Also

Reference

Other Resources

Page view tracker