C# winform基于 BackgroundWorker实现异步复制文件显示进度条百分比源代码
作者:野牛程序员:2023-12-28 20:04:48C#阅读 2897
C# winform基于 BackgroundWorker实现异步复制文件显示进度条百分比源代码
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace FileCopyApp
{
public partial class MainForm : Form
{
private BackgroundWorker backgroundWorker;
private string sourceFilePath;
private string destinationFolderPath;
public MainForm()
{
InitializeComponent();
InitializeBackgroundWorker();
}
private void InitializeBackgroundWorker()
{
backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
backgroundWorker.DoWork += BackgroundWorker_DoWork;
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
}
private void btnBrowseSource_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Title = "Select Source File";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
sourceFilePath = openFileDialog.FileName;
txtSourcePath.Text = sourceFilePath;
}
}
}
private void btnBrowseDestination_Click(object sender, EventArgs e)
{
using (FolderDialog folderDialog = new FolderDialog())
{
folderDialog.Title = "Select Destination Folder";
if (folderDialog.ShowDialog() == DialogResult.OK)
{
destinationFolderPath = folderDialog.SelectedPath;
txtDestinationPath.Text = destinationFolderPath;
}
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(sourceFilePath) && !string.IsNullOrEmpty(destinationFolderPath))
{
btnCopy.Enabled = false;
backgroundWorker.RunWorkerAsync();
}
else
{
MessageBox.Show("Please select source file and destination folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int bufferSize = 1024 * 1024; // 1 MB buffer size
byte[] buffer = new byte[bufferSize];
using (FileStream sourceStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
{
using (FileStream destinationStream = new FileStream(Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath)), FileMode.Create, FileAccess.Write))
{
long totalBytes = sourceStream.Length;
long copiedBytes = 0;
int bytesRead;
while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
destinationStream.Write(buffer, 0, bytesRead);
copiedBytes += bytesRead;
int progressPercentage = (int)((copiedBytes * 100) / totalBytes);
backgroundWorker.ReportProgress(progressPercentage);
}
}
}
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnCopy.Enabled = true;
if (e.Error != null)
{
MessageBox.Show($"Error: {e.Error.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("File copied successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

