c#下载进度条

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值