C# 如何使用progressbar进行下载过程

C# 如何使用progressbar进行下载过程,c#,winforms,download,progress-bar,C#,Winforms,Download,Progress Bar,我想在windows窗体应用程序中使用进度条。该应用程序将用于将文件从一个目录下载到另一个目录 但当用户单击下载按钮时,应用程序似乎什么也不做。所以我想向用户展示使用进度条向下移动的过程 我确实搜索了进度条,但我找不到“如何使用进度条进行下载过程”的答案 如果有人向我解释如何使用进度条进行下载,我将非常高兴。网络客户端有一个DownloadProgressChanged-事件。把它连接到进度条上,你就可以开始了 备注:您需要使用DownloadDataAsync、DownloadFileAsyn

我想在windows窗体应用程序中使用进度条。该应用程序将用于将文件从一个目录下载到另一个目录

但当用户单击下载按钮时,应用程序似乎什么也不做。所以我想向用户展示使用进度条向下移动的过程

我确实搜索了进度条,但我找不到“如何使用进度条进行下载过程”的答案


如果有人向我解释如何使用进度条进行下载,我将非常高兴。

网络客户端有一个
DownloadProgressChanged
-事件。把它连接到进度条上,你就可以开始了


备注:您需要使用
DownloadDataAsync
DownloadFileAsync
OpenReadAsync
来触发
DownloadProgressChanged
-事件。

WebClient
具有
DownloadProgressChanged
-事件。把它连接到进度条上,你就可以开始了


备注:您需要使用
DownloadDataAsync
DownloadFileAsync
OpenReadAsync
来触发
DownloadProgressChanged
-事件。

WebClient
具有
DownloadProgressChanged
-事件。把它连接到进度条上,你就可以开始了


备注:您需要使用
DownloadDataAsync
DownloadFileAsync
OpenReadAsync
来触发
DownloadProgressChanged
-事件。

WebClient
具有
DownloadProgressChanged
-事件。把它连接到进度条上,你就可以开始了


备注:您需要使用
DownloadDataAsync
DownloadFileAsync
OpenReadAsync
来触发
DownloadProgressChanged
-事件。

您可以使用DownloadFileAsync来下载文件,而不阻塞主线程,还可以设置事件处理程序以在条形图中显示进度:

  private void button1_Click(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        string sourceFile = @"\\server\test.txt";
        string destFile = @"\\server2\test2.txt";
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(sourceFile), destFile);
    }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("The download is completed!");
    }
或者,另一种方法可以使用属性WorkerReportsProgress设置为true的BackgroundWorker。然后,您应该订阅事件DoWorkProgressChanged:在DoWork方法中,您将代码放在单独的线程上下载或传输文件,并计算工作进度。在ProgressChanged方法中,只需更新进度条值。在这种情况下,您的代码如下所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // the path of the source file
        string sourceFile = @"\\shared\test.txt";
        // the path to write the file to
        string destFile = @"\\shared2\test2.txt";

        FileInfo info = new FileInfo(sourceFile);
        // gets the size of the file in bytes
        Int64 size = info.Length;
        // keeps track of the total bytes downloaded so you can update the progress bar
        Int64 runningByteTotal = 0;
        using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
        {
            using (Stream writer = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[size];
                while ((iByteSize = reader.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    // write the bytes to the file
                    writer.Write(byteBuffer, 0, iByteSize);
                    runningByteTotal += iByteSize;
                    // calculate the progress
                    double index = (double)(runningByteTotal);
                    double total = (double)byteBuffer.Length;
                    double progressPercentage = (index / total);
                    int iProgressPercentage = (int)(progressPercentage * 100);
                    // update the progress bar
                    backgroundWorker1.ReportProgress(iProgressPercentage);
                }
                // clean up the file stream
                writer.Close();
            }

        }
    }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
在触发文件下载的按钮单击事件(或其他事件)中,应添加以下代码以启动后台工作程序异步运行:

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

您可以使用DownloadFileAsync在不阻塞主线程的情况下下载文件,还可以设置一个事件处理程序以在栏中显示进度:

  private void button1_Click(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        string sourceFile = @"\\server\test.txt";
        string destFile = @"\\server2\test2.txt";
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(sourceFile), destFile);
    }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("The download is completed!");
    }
或者,另一种方法可以使用属性WorkerReportsProgress设置为true的BackgroundWorker。然后,您应该订阅事件DoWorkProgressChanged:在DoWork方法中,您将代码放在单独的线程上下载或传输文件,并计算工作进度。在ProgressChanged方法中,只需更新进度条值。在这种情况下,您的代码如下所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // the path of the source file
        string sourceFile = @"\\shared\test.txt";
        // the path to write the file to
        string destFile = @"\\shared2\test2.txt";

        FileInfo info = new FileInfo(sourceFile);
        // gets the size of the file in bytes
        Int64 size = info.Length;
        // keeps track of the total bytes downloaded so you can update the progress bar
        Int64 runningByteTotal = 0;
        using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
        {
            using (Stream writer = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[size];
                while ((iByteSize = reader.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    // write the bytes to the file
                    writer.Write(byteBuffer, 0, iByteSize);
                    runningByteTotal += iByteSize;
                    // calculate the progress
                    double index = (double)(runningByteTotal);
                    double total = (double)byteBuffer.Length;
                    double progressPercentage = (index / total);
                    int iProgressPercentage = (int)(progressPercentage * 100);
                    // update the progress bar
                    backgroundWorker1.ReportProgress(iProgressPercentage);
                }
                // clean up the file stream
                writer.Close();
            }

        }
    }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
在触发文件下载的按钮单击事件(或其他事件)中,应添加以下代码以启动后台工作程序异步运行:

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

您可以使用DownloadFileAsync在不阻塞主线程的情况下下载文件,还可以设置一个事件处理程序以在栏中显示进度:

  private void button1_Click(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        string sourceFile = @"\\server\test.txt";
        string destFile = @"\\server2\test2.txt";
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(sourceFile), destFile);
    }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("The download is completed!");
    }
或者,另一种方法可以使用属性WorkerReportsProgress设置为true的BackgroundWorker。然后,您应该订阅事件DoWorkProgressChanged:在DoWork方法中,您将代码放在单独的线程上下载或传输文件,并计算工作进度。在ProgressChanged方法中,只需更新进度条值。在这种情况下,您的代码如下所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // the path of the source file
        string sourceFile = @"\\shared\test.txt";
        // the path to write the file to
        string destFile = @"\\shared2\test2.txt";

        FileInfo info = new FileInfo(sourceFile);
        // gets the size of the file in bytes
        Int64 size = info.Length;
        // keeps track of the total bytes downloaded so you can update the progress bar
        Int64 runningByteTotal = 0;
        using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
        {
            using (Stream writer = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[size];
                while ((iByteSize = reader.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    // write the bytes to the file
                    writer.Write(byteBuffer, 0, iByteSize);
                    runningByteTotal += iByteSize;
                    // calculate the progress
                    double index = (double)(runningByteTotal);
                    double total = (double)byteBuffer.Length;
                    double progressPercentage = (index / total);
                    int iProgressPercentage = (int)(progressPercentage * 100);
                    // update the progress bar
                    backgroundWorker1.ReportProgress(iProgressPercentage);
                }
                // clean up the file stream
                writer.Close();
            }

        }
    }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
在触发文件下载的按钮单击事件(或其他事件)中,应添加以下代码以启动后台工作程序异步运行:

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

您可以使用DownloadFileAsync在不阻塞主线程的情况下下载文件,还可以设置一个事件处理程序以在栏中显示进度:

  private void button1_Click(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        string sourceFile = @"\\server\test.txt";
        string destFile = @"\\server2\test2.txt";
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(sourceFile), destFile);
    }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("The download is completed!");
    }
或者,另一种方法可以使用属性WorkerReportsProgress设置为true的BackgroundWorker。然后,您应该订阅事件DoWorkProgressChanged:在DoWork方法中,您将代码放在单独的线程上下载或传输文件,并计算工作进度。在ProgressChanged方法中,只需更新进度条值。在这种情况下,您的代码如下所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // the path of the source file
        string sourceFile = @"\\shared\test.txt";
        // the path to write the file to
        string destFile = @"\\shared2\test2.txt";

        FileInfo info = new FileInfo(sourceFile);
        // gets the size of the file in bytes
        Int64 size = info.Length;
        // keeps track of the total bytes downloaded so you can update the progress bar
        Int64 runningByteTotal = 0;
        using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
        {
            using (Stream writer = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[size];
                while ((iByteSize = reader.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    // write the bytes to the file
                    writer.Write(byteBuffer, 0, iByteSize);
                    runningByteTotal += iByteSize;
                    // calculate the progress
                    double index = (double)(runningByteTotal);
                    double total = (double)byteBuffer.Length;
                    double progressPercentage = (index / total);
                    int iProgressPercentage = (int)(progressPercentage * 100);
                    // update the progress bar
                    backgroundWorker1.ReportProgress(iProgressPercentage);
                }
                // clean up the file stream
                writer.Close();
            }

        }
    }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
在触发文件下载的按钮单击事件(或其他事件)中,应添加以下代码以启动后台工作程序异步运行:

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

如海报所述,此答案对WinForms应用程序有效吗?感谢您的回答,但此答案是否可用于将文件从一个目录传输到另一个目录,如\\computername\sharefolder到\\computername2\sharefolder2?我认为它可用于将文件从url传输到一个目录。我说的对吗?
WebClient
只对文件有效。这些文件可以位于本地或远程目标。您可以编写自己的复制目录方法,分别下载所有文件。此答案是否适用于海报中解释的WinForms应用程序?谢谢您的回答,但这是否可用于将文件从目录传输到另一个目录,例如\\computername\sharefolder到\\computername2\sharefolder2?我想是这样的可用于将文件从url传输到目录。我说的对吗?
W