SPFile.OpenBinary method

Opens the file in binary format.

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

public byte[] OpenBinary()

Return value

Type: []
A byte array that contains the file content.

The following code example iterates through the collection of files in the Shared Documents document library of the current site and opens each file in binary format so that it can be attached to a specified item in the Events list. The OpenBinary method fails if the size of the file is 0 (zero) bytes.

SPWeb oWebsite = SPContext.Current.Web;
SPFolder oFolder = oWebsite.Folders["Shared Documents"];

SPFileCollection collFiles = oFolder.Files;
SPList oList = oWebsite.Lists["Events"];
SPListItem oListItem = oList.Items[10];
SPAttachmentCollection collAttachments = oListItem.Attachments;

foreach (SPFile oFile in collFiles)
    string strFilename = oFile.Name;
    byte[] binFile = oFile.OpenBinary();
    collAttachments.Add(strFilename, binFile);