如何在WinForms中制作文件下载进度栏?

时间:2019-10-21 18:27:23

标签: winforms download progress-bar

我正在使用CefSharp创建网络浏览器。我刚刚在两天前实现了下载,但是没有进度条与下载一起进行。如何使进度条显示下载进度?

1 个答案:

答案 0 :(得分:0)

编辑:让事情变得更清楚

在表单中添加一个ProgressBar控件,并在其旁边添加一个BackgroundWorker组件。首先确定您的文件大小:

Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])

这是Alex惊人的答案中的代码,可以在这里找到:

public Form1()
{
    InitializeComponent();

    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    backgroundWorker1.WorkerReportsProgress = true;
}

private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    //Replace this code with a way to figure out how much of the file you have already downloaded and then use backgroundworker1.ReportProgress and send the percentage completion of the file download.
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(1000);
        backgroundWorker1.ReportProgress(i);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}