从SharePoint,我得到了一个"流";对于文件。我想将整个文件从互联网复制到电脑上的本地文件,但我想在下载过程中保持状态。怎样FileStream和StreamReader在不执行完整的";CopyTo";它没有进度更新。必须有一种更清洁的方式。。。
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);
}
}
}
只需要使用BinaryReader而不是StreamReader。Easy Peasy。