You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio C# WinForms中为下载功能添加进度条

在C# Windows Forms中为WebClient下载添加进度条跟踪

步骤1:准备界面控件

在你的Windows Forms窗体中添加以下控件:

  • 一个ProgressBar控件(命名为progressBar1),用于可视化显示下载进度
  • (可选)一个Label控件(命名为lblProgress),用于显示具体的进度百分比文本

步骤2:修改下载代码(异步+进度事件)

原代码使用的同步DownloadFile方法会阻塞UI线程,导致界面假死无法更新进度。我们改用异步下载方法,并通过WebClient的内置事件实现进度跟踪和完成回调。

完整代码示例:

private void btnStartDownload_Click(object sender, EventArgs e)
{
    string url = "https://discovertheme.net/dosyalar/Appserv%208.6.0.exe";
    string downloadsFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string fileName = "Appserv 8.6.0.exe";
    string filePath = Path.Combine(downloadsFolder, "Downloads", fileName);

    // 初始化进度条状态
    progressBar1.Minimum = 0;
    progressBar1.Maximum = 100;
    progressBar1.Value = 0;
    lblProgress.Text = "0%";

    using (WebClient webClient = new WebClient())
    {
        // 绑定进度变更事件
        webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
        // 绑定下载完成事件
        webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;

        // 启动异步下载
        webClient.DownloadFileAsync(new Uri(url), filePath);
    }
}

// 进度变更事件处理逻辑
private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    // WinForms控件只能在UI线程更新,需判断是否跨线程
    if (progressBar1.InvokeRequired)
    {
        progressBar1.Invoke(new Action(() =>
        {
            progressBar1.Value = e.ProgressPercentage;
            lblProgress.Text = $"{e.ProgressPercentage}%";
        }));
    }
    else
    {
        progressBar1.Value = e.ProgressPercentage;
        lblProgress.Text = $"{e.ProgressPercentage}%";
    }
}

// 下载完成事件处理逻辑
private void WebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        MessageBox.Show($"Dosyayı yüklenirken bir hata oluştu: {e.Error.Message}", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if (e.Cancelled)
    {
        MessageBox.Show("İndirme iptal edildi!", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show("İndirme başarıyla yüklendi!", "Bildirim", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    // 重置进度条状态
    if (progressBar1.InvokeRequired)
    {
        progressBar1.Invoke(new Action(() =>
        {
            progressBar1.Value = 0;
            lblProgress.Text = "0%";
        }));
    }
    else
    {
        progressBar1.Value = 0;
        lblProgress.Text = "0%";
    }
}

关键说明

  • 异步下载:DownloadFileAsync方法不会阻塞UI线程,确保界面始终可响应,同时能实时接收进度更新。
  • 进度事件:DownloadProgressChanged事件会在下载过程中持续触发,通过DownloadProgressChangedEventArgs可以获取当前进度百分比、已下载字节数和总字节数。
  • 跨线程UI更新:WinForms控件的操作必须在创建它们的UI线程中执行,因此需要用Invoke方法切换线程,避免线程安全异常。

内容的提问来源于stack exchange,提问作者Shark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:43:18