SPFileCollection.Add method (String, Byte[], Boolean)

Creates a file in the collection using the specified URL, a byte array that contains the contents of a file, and a Boolean value that specifies whether to overwrite any file that has the same name.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function Add ( _
    urlOfFile As String, _
    file As Byte(), _
    overwrite As Boolean _
) As SPFile
'Usage
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
Dim overwrite As Boolean
Dim returnValue As SPFile

returnValue = instance.Add(urlOfFile, _
    file, overwrite)
public SPFile Add(
    string urlOfFile,
    byte[] file,
    bool overwrite
)

Parameters

  • urlOfFile
    Type: System.String

    The site-relative URL of the file.

  • file
    Type: []

    A byte array that contains the file.

  • overwrite
    Type: System.Boolean

    true to overwrite a file of the same name; otherwise, false.

Return value

Type: Microsoft.SharePoint.SPFile
The newly added file.

Examples

The following code example iterates through the collection of files in a document library of a site and, if the check-in comment for a file is "Finished", copies the file to a specified folder.

Dim SiteCollection As New SPSite("http://MySiteCollection")
Try
    Dim srcFolder As SPFolder = siteCollection.AllWebs("MySourceWebSite").GetFolder("MySourceLib")

    Dim destFiles As SPFileCollection = siteCollection.AllWebs("MyDestWebSite").GetFolder("MyDestFolder").Files

    Dim srcFile As SPFile
    For Each srcFile In  srcFolder.Files
        If srcFile.CheckInComment = "Finished" Then
            Dim destURL As String = destFiles.Folder.Url + "/" + srcFile.Name
            Dim binFile As Byte() = srcFile.OpenBinary()

            destFiles.Add(destURL, binFile, True)
        End If
    Next srcFile
Finally
    SiteCollection.Dispose()
End Try
using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
    SPFolder oFolder = oSiteCollection.AllWebs["SourceWebSite"].GetFolder("MySourceLib");
    SPFileCollection collFiles = 
oSiteCollection.AllWebs["DestWebSite"].GetFolder("DestFolder").Files;
    foreach (SPFile oFile in collFiles.Files)
    {
        if (oFile.CheckInComment == "Finished")
        {
            string strDestUrl = collFiles.Folder.Url + 
                "/" + oFile.Name;
            byte[] binFile = oFile.OpenBinary();
            collFiles.Add(strDestUrl, binFile, true);
        }
    }
}

Note

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 Disposing Objects.

See also

Reference

SPFileCollection class

SPFileCollection members

Add overload

Microsoft.SharePoint namespace