您需要这样的东西:
public delegate void ProgressChangeDelegate(double Persentage, ref bool Cancel); public delegate void Completedelegate(); class CustomFileCopier { public CustomFileCopier(string Source, string Dest) { this.SourceFilePath = Source; this.DestFilePath = Dest; onProgressChanged += delegate { }; onComplete += delegate { }; } public void Copy() { byte[] buffer = new byte[1024 * 1024]; // 1MB buffer bool cancelFlag = false; using (FileStream source = new FileStream(SourceFilePath, FileMode.Open, FileAccess.Read)) { long fileLength = source.Length; using (FileStream dest = new FileStream(DestFilePath, FileMode.CreateNew, FileAccess.Write)) { long totalBytes = 0; int currentBlockSize = 0; while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0) { totalBytes += currentBlockSize; double persentage = (double)totalBytes * 100.0 / fileLength; dest.Write(buffer, 0, currentBlockSize); cancelFlag = false; onProgressChanged(persentage, ref cancelFlag); if (cancelFlag == true) { // Delete dest file here break; } } } } onComplete(); } public string SourceFilePath { get; set; } public string DestFilePath { get; set; } public event ProgressChangeDelegate OnProgressChanged; public event Completedelegate OnComplete; }只需在单独的线程中运行它并订阅
OnProgressChanged事件即可。



