This documentation is archived and is not being maintained.

AddFromFile Method (ProjectItems Collection)

Visual Studio .NET 2003

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

[Visual Basic .NET]
Public Function AddFromFile( _
   ByVal FileName As String _
) As ProjectItem
[Visual Basic 6]
Function AddFromFile( _
   ByVal FileName As String _
) As ProjectItem
HRESULT __stdcall AddFromFile(
   BSTR FileName,
   /* [out, retval] */ ProjectItem** retVal
ProjectItem AddFromFile(
   string FileName
[JScript .NET]
public function AddFromFile(
   FileName : String
) : ProjectItem


Required. The file name of the item to add as a project item.

Return Value

Returns a ProjectItem object.


This method should fail if the file to be added is already in the project.

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


The following example is designed to be run in Macros IDE. Before running this example, create a file named "An Item.bas" in the path C:\temp.

Sub AddFromFileExample()
   ' This function creates a solution and adds a Visual Basic .NET 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 VB 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

See Also

AddFromDirectory Method | AddFromFileCopy Method | AddFromTemplate Method (ProjectItems Object) | Open Method (General Extensibility)

Applies To: ProjectItems Collection