1

From SharePoint, I get a "Stream" for a file. I want to copy this entire file from the internet to a local file on the PC, but I want to have status while this download is occurring. How? FileStream and StreamReader seem to have bytes vs char differences when not doing a full "CopyTo" which doesn't have progress updates. There has to be a cleaner way...

using (var sr = new StreamReader(fileData.Value))
{
    using (FileStream fs = new FileStream(localFile + "_tmp", FileMode.Create))
    {
        byte[] block = new byte[1024];
        // only guessing something like this is necessary...
        int count = (int)Math.Min(sr.BaseStream.Length - sr.BaseStream.Position, block.Length);
        while (sr.BaseStream.Position < sr.BaseStream.Length)
        {
            // read  requires char[]
            sr.Read(block, 0, count);
            // write requires byte[]
            fs.Write(block, 0, count);
            Log("Percent complete: " + (sr.BaseStream.Position / sr.BaseStream.Length));
            count = (int)Math.Min(sr.BaseStream.Length - sr.BaseStream.Position, block.Length);
        }
    }
}

CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
  • Does this answer your question? [How to report progress from FileStream](https://stackoverflow.com/questions/57432133/how-to-report-progress-from-filestream) – kshkarin Mar 26 '21 at 18:43
  • Nope. Already tried that. Doesn't seem to work for StreamReader even after trying to modify it. – CodeMonkey Mar 26 '21 at 18:55

1 Answers1

0

Just had to use BinaryReader instead of StreamReader. Easy Peasy.

CodeMonkey
  • 1,795
  • 3
  • 16
  • 46