Represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Use the GetFile or GetFileAsString method of the SPWeb class to return a single file object. Otherwise, use the Files property of either the SPWeb or SPFolder class to return an SPFileCollection object that represents the collection of files for a site or folder. Use an indexer to return a single file from the collection. For example, if the collection is assigned to a variable named collFiles, use collFiles[index] in C#, or collFiles(index) in Visual Basic, where index is the index number of the file in the collection, the file name including extension, or a Web site relative, site collection relative, or absolute URL.
This example adds a file from the document library of one site to the Shared Documents document library of another site and its subsites.
For an example that shows how to upload a local file to a folder on a SharePoint site programmatically, see How to: Upload a File to a SharePoint Site from a Local Folder.
Dim siteCollection As SPSite = SPContext.Current.Site Dim srcSite As SPWeb = siteCollection.AllWebs("Source_Site_Name") Dim destSites As SPWebCollection = siteCollection.AllWebs("Destination_Site_Name").Webs Dim srcFile As SPFile = srcSite.GetFile("Source_Folder_Name/Source_File") Dim fileName As String = srcFile.Name Dim binFile As Byte() = srcFile.OpenBinary() Dim destSite As SPWeb For Each destSite In destSites If destSite.GetFolder("Shared Documents").Exists Then Dim destFolder As SPFolder = destSite.GetFolder("Shared Documents") destFolder.Files.Add(fileName, binFile, True) End If Next destSite
Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.