C# WebClient上传下载时进度条显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace WebClientUpload
{
    public partial class Form3 : Form
    {
        WebClient c;
        public Form3()
        {
            InitializeComponent();
            c = new System.Net.WebClient();
            c.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(c_DownloadProgressChanged);
            c.DownloadFileCompleted += new AsyncCompletedEventHandler(c_DownloadFileCompleted);
            c.Proxy = WebRequest.DefaultWebProxy;
            //c.Proxy.Credentials = new NetworkCredential("admin", "admin_password", "domain");

        }

        void c_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("ok");
            this.Close();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
           
        }

        void c_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            this.label1.Text = "已完成:" + progressBar1.Value.ToString()+"%";
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
           
        }


        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            //上传文件
            c.DownloadFileAsync(new Uri("d:\\download\\阿甘正传.rm"), @"e:\11.rm");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //取消上传
            if (c != null)
            {
                c.CancelAsync();
            }
            Close(); 
        }
    }
}

 

 

 

附:另种进度条控制方案:

Form1调用 Form2(进度条窗体)         

   Form2 pj = new Form2();
            pj.Show();

            //开始处理大量耗时工作
            string sor = "";
            int j = 100000;
            for (int i = 0; i < j; i++)
            {
                sor += i.ToString();
                if (i % (j / 100) == 0)
                {
                    pj.progressBar1.Value++;
                    pj.label2.Text = "已完成:" + string.Format("{0:p}", (double)pj.progressBar1.Value / 100);
                    Application.DoEvents();
                }
            }
            //工作完成,关闭进度条窗体
            pj.Close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,你可以使用WebClient类来下载文件并限制下载速度。你需要使用System.Threading命名空间中的Thread.Sleep方法来暂停下载,并将速度限制为所需的速度。 以下是一个简单的示例代码,它将下载速度限制为10KB/s: ``` using System.Net; using System.Threading; public void DownloadFile(string url, string fileName) { WebClient client = new WebClient(); client.DownloadProgressChanged += WebClient_DownloadProgressChanged; client.DownloadFileCompleted += WebClient_DownloadFileCompleted; using (var stream = client.OpenRead(url)) using (var output = File.Create(fileName)) { byte[] buffer = new byte[1024]; int bytesRead = 0; int downloadSpeed = 10 * 1024; // 10KB/s DateTime startTime = DateTime.Now; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, bytesRead); // 计算已下载的字节数和间 long bytesDownloaded = output.Position; double secondsElapsed = (DateTime.Now - startTime).TotalSeconds; // 计算当前下载速度,并暂停下载 int currentSpeed = (int)(bytesDownloaded / secondsElapsed); if (currentSpeed > downloadSpeed) { int sleepTime = (int)(1000 * ((double)bytesRead / downloadSpeed - secondsElapsed)); if (sleepTime > 0) Thread.Sleep(sleepTime); } } } } private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { // 下载进度改变的操作 } private void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { // 下载完成的操作 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值