如何从 FileStream 报告进度

我想报告加密文件的进度,这是我的代码,我该怎么做?


using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))

using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))

using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))

{

    await source.CopyToAsync(cryptoStream);

}


四季花海
浏览 85回答 1
1回答

互换的青春

要正确执行此操作,您需要插入另一个流来报告进度。所以像这样的事情...&nbsp; &nbsp; &nbsp; &nbsp; using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))&nbsp; &nbsp; &nbsp; &nbsp; using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))&nbsp; &nbsp; &nbsp; &nbsp; using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (ProgressStream progressStream = new ProgressStream(source))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressStream.UpdateProgress += ProgressStream_UpdateProgress;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressStream.CopyTo(cryptoStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }ProgressStream 在哪里...public class ProgressStream : Stream{&nbsp; &nbsp; private Stream m_input = null;&nbsp; &nbsp; private long m_length = 0L;&nbsp; &nbsp; private long m_position = 0L;&nbsp; &nbsp; public event EventHandler<ProgressEventArgs> UpdateProgress;&nbsp; &nbsp; public ProgressStream(Stream input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; m_input = input;&nbsp; &nbsp; &nbsp; &nbsp; m_length = input.Length;&nbsp; &nbsp; }&nbsp; &nbsp; public override void Flush()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new System.NotImplementedException();&nbsp; &nbsp; }&nbsp; &nbsp; public override long Seek(long offset, SeekOrigin origin)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new System.NotImplementedException();&nbsp; &nbsp; }&nbsp; &nbsp; public override void SetLength(long value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new System.NotImplementedException();&nbsp; &nbsp; }&nbsp; &nbsp; public override int Read(byte[] buffer, int offset, int count)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int n = m_input.Read(buffer, offset, count);&nbsp; &nbsp; &nbsp; &nbsp; m_position += n;&nbsp; &nbsp; &nbsp; &nbsp; UpdateProgress?.Invoke(this, new ProgressEventArgs((1.0f * m_position)/m_length));&nbsp; &nbsp; &nbsp; &nbsp; return n;&nbsp; &nbsp; }&nbsp; &nbsp; public override void Write(byte[] buffer, int offset, int count)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new System.NotImplementedException();&nbsp; &nbsp; }&nbsp; &nbsp; public override bool CanRead => true;&nbsp; &nbsp; public override bool CanSeek => false;&nbsp; &nbsp; public override bool CanWrite => false;&nbsp; &nbsp; public override long Length => m_length;&nbsp; &nbsp; public override long Position&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; return m_position; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; throw new System.NotImplementedException();}&nbsp; &nbsp; }}ProgressEventArgs 是public class ProgressEventArgs : EventArgs{&nbsp; &nbsp; private float m_progress;&nbsp; &nbsp; public ProgressEventArgs(float progress)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; m_progress = progress;&nbsp; &nbsp; }&nbsp; &nbsp; public float Progress => m_progress;}事件处理程序可能是这样的......&nbsp; &nbsp; private void ProgressStream_UpdateProgress(object sender, ProgressEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"Progress is {e.Progress * 100.0f}%");&nbsp; &nbsp; }当针对示例文件运行时会产生......Progress is 5.272501%Progress is 10.545%Progress is 15.8175%Progress is 21.09%Progress is 26.3625%Progress is 31.635%Progress is 36.9075%Progress is 42.18%Progress is 47.4525%Progress is 52.72501%Progress is 57.99751%Progress is 63.27001%Progress is 68.5425%Progress is 73.815%Progress is 79.08751%Progress is 84.36001%Progress is 89.63251%Progress is 94.90501%Progress is 100%Progress is 100%有很大的增强和优化空间,但这是做你想做的事情的唯一有效方法。
打开App,查看更多内容
随时随地看视频慕课网APP