This documentation is archived and is not being maintained.

SPFileCollection.Add Method (String, Byte[])

Windows SharePoint Services 3

Creates a file in the collection based on the specified URL and on 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
A string that specifies the URL for the file.
Type: []
A byte array containing the file.

Return Value

Type: Microsoft.SharePoint.SPFile
An SPFile object that represents the 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 Best Practices: Using Disposable Windows SharePoint Services Objects.