SPFileCollection.Add method (String, Byte[])

Creates a file in the collection using the specified URL and a byte array that contains the file.

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

public SPFile Add(
	string urlOfFile,
	byte[] file


Type: System.String

The site-relative URL of the file.

Type: []

A byte array that contains the file.

Return value

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

The following code example uses the Add method to copy a file from a document library to the Shared Documents folder of every subsite under a specified Web site. If a file of the same name already exists, the Add method throws an exception.

using (SPSite oSiteCollection = new SPSite("http://localhost"))
    using(SPWeb oWebsite = oSiteCollection.AllWebs["MyWebSite"])
        SPWebCollection collWebsites = oWebsite.Webs;
        SPFile oFile = oWebsite.GetFile("MyDocLib/MyFile");
        foreach (SPWeb oWebsiteNext in collWebsites)
            SPFolder oFolder = oWebsiteNext.GetFolder("Shared Documents");
            SPFileCollection collFiles = oFolder.Files;

            string strDestUrl = oFolder.Url + "/" + oFile.Name;
            byte[] binFile = oFile.OpenBinary();
            collFiles.Add(strDestUrl, binFile);


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.