Downloading a SharePoint File

One way to download a file from SharePoint is to call Microsoft.SharePoint.Client.File.OpenBinaryDirect. Since we don’t usually know the size of the file before we open it (or don’t want to check ahead just to read), we can’t use our stream’s Length property to figure out how many bytes we need to allocate. Instead, we’ll just read the file in chunks through a MemoryStream and convert it into an array at the end:

I believe streams in .Net 4.0 can be copied using the new .CopyTo method. For very large files or slow network connections you may need to consider processing in a worker thread. If you don’t want to use the SharePoint Client API, in some cases the WebClient class’s downloading methods may be enough to do the job.