我正在尝试使用c#winforms应用中的进度条实现文件副本。 现在只需一个线程。 我发现文件传递达到100%并在filestream.close()上挂起了很长时间。 目的地是UNC路径。
我的实施有什么问题?
// Create source and destination file streams
using (var inputFileStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
using (var outputFileStream = new FileStream(destPath + Path.GetFileNameWithoutExtension(sourceFilePath) + ".tfr", FileMode.Create))
{
long sourceFileSize = inputFileStream.Length;
int offset = 0;
int bufferSize = 1024;
var buffer = new byte[bufferSize];
int progressPercent;
while(offset < sourceFileSize)
{
// Fill buffer
inputFileStream.Read(buffer, 0, bufferSize);
// Empty buffer to output stream
outputFileStream.Write(buffer, 0, bufferSize);
// Flush buffer
//Array.Clear(buffer, 0, buffer.Length);
// Advance read offset
offset += bufferSize;
// Update GUI
progressPercent = Convert.ToInt32(((double)offset / (double)sourceFileSize) * 100.0);
pBar.Value = progressPercent;
pBar.Refresh();
labUploadStatus.Text = $"Uploading file...{progressPercent}%";
labUploadStatus.Refresh();
}
outputFileStream.Close();
inputFileStream.Close();
}