如何从 FileStream 报告进度
How to report progress from 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);
}
要正确执行此操作,您需要插入另一个流来报告进度。所以像这样...
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))
{
using (ProgressStream progressStream = new ProgressStream(source))
{
progressStream.UpdateProgress += ProgressStream_UpdateProgress;
progressStream.CopyTo(cryptoStream);
}
}
ProgressStream 在哪里...
public class ProgressStream : Stream
{
private Stream m_input = null;
private long m_length = 0L;
private long m_position = 0L;
public event EventHandler<ProgressEventArgs> UpdateProgress;
public ProgressStream(Stream input)
{
m_input = input;
m_length = input.Length;
}
public override void Flush()
{
throw new System.NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value)
{
throw new System.NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
int n = m_input.Read(buffer, offset, count);
m_position += n;
UpdateProgress?.Invoke(this, new ProgressEventArgs((1.0f * m_position)/m_length));
return n;
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotImplementedException();
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => m_length;
public override long Position
{
get { return m_position; }
set { throw new System.NotImplementedException();}
}
}
ProgressEventArgs 是
public class ProgressEventArgs : EventArgs
{
private float m_progress;
public ProgressEventArgs(float progress)
{
m_progress = progress;
}
public float Progress => m_progress;
}
事件处理程序可能是这样的...
private void ProgressStream_UpdateProgress(object sender, ProgressEventArgs e)
{
Console.WriteLine($"Progress is {e.Progress * 100.0f}%");
}
当 运行 针对示例文件生成...
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%
增强和优化的空间很大,但这是做您想做的事情的唯一有效方法。
我想报告加密文件的进度这是我的代码我该怎么做?
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);
}
要正确执行此操作,您需要插入另一个流来报告进度。所以像这样...
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))
{
using (ProgressStream progressStream = new ProgressStream(source))
{
progressStream.UpdateProgress += ProgressStream_UpdateProgress;
progressStream.CopyTo(cryptoStream);
}
}
ProgressStream 在哪里...
public class ProgressStream : Stream
{
private Stream m_input = null;
private long m_length = 0L;
private long m_position = 0L;
public event EventHandler<ProgressEventArgs> UpdateProgress;
public ProgressStream(Stream input)
{
m_input = input;
m_length = input.Length;
}
public override void Flush()
{
throw new System.NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value)
{
throw new System.NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
int n = m_input.Read(buffer, offset, count);
m_position += n;
UpdateProgress?.Invoke(this, new ProgressEventArgs((1.0f * m_position)/m_length));
return n;
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotImplementedException();
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => m_length;
public override long Position
{
get { return m_position; }
set { throw new System.NotImplementedException();}
}
}
ProgressEventArgs 是
public class ProgressEventArgs : EventArgs
{
private float m_progress;
public ProgressEventArgs(float progress)
{
m_progress = progress;
}
public float Progress => m_progress;
}
事件处理程序可能是这样的...
private void ProgressStream_UpdateProgress(object sender, ProgressEventArgs e)
{
Console.WriteLine($"Progress is {e.Progress * 100.0f}%");
}
当 运行 针对示例文件生成...
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%
增强和优化的空间很大,但这是做您想做的事情的唯一有效方法。