I would like to know what are benefits/drawbacks when using either of these methods for downloading a document from Sharepoint

using Microsoft sharepoint library

public void Download(string serverFilePath, string destPath) {
    using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_clientCtx, serverFilePath)) {
        using (Stream destFile = System.IO.File.OpenWrite(destPath)) {
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0) {
                destFile.Write(buffer, 0, len);

as oposed to downloading it using WebRequest

public void DownloadFile(string serverFilePath, string destPath) {
    var url = string.Format("{0}/{1}", ServerURL, serverFilePath);
    var request = System.Net.HttpWebRequest.Create(url);
    request.Credentials = _clientCtx.Credentials;
    using (var sr = new StreamReader(request.GetResponse().GetResponseStream())) {
        using (var sw = new StreamWriter(destPath)) {

because using the HttpRequest results in approximately 30% better performance.

Thanks in advance.


I don't mean to nitpick but your first example is a really bad approach. You are reallocating your buffer every iteration and using a truly tiny buffer at that. Try this approach instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.