方法一:

WebClient webClint = new WebClient();

webClint.DownloadFile(下载文件的路径,下载到本地的地址);

方法二:异步

WebClient webClint;
        float maxSpeed = 0;
        float currentSpeed = 0;
        float lastSpeed = 0;
        long lastReceive = 0;
        long reeciveSpan;
        TimeSpan span;
        DateTime start;
        DateTime lastTime;
        TimeSpan useTime;
        protected void Button1_Click(object sender, EventArgs e)
        {

webClint.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClint_DownloadProgressChanged);
  webClint.DownloadFileAsync(new Uri下载文件的路径, 下载到本地的地址);

}

void webClint_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            span = DateTime.Now - lastTime;
            lastTime = DateTime.Now;
            useTime = lastTime - start;
            reeciveSpan = e.BytesReceived - lastReceive;
            currentSpeed = reeciveSpan * 1000 / (span.Milliseconds + 1) / 1024;
            maxSpeed = currentSpeed > maxSpeed ? currentSpeed : maxSpeed;
            lastSpeed = currentSpeed;
            lastReceive = e.BytesReceived;
           // progressBar1.Value = e.ProgressPercentage;
            this.Label1.Text = "已下载: " + e.BytesReceived / 1024 + "KB\n" +
                "总大小: " + e.TotalBytesToReceive / 1024 + "KB\n" +
                "百分比: " + e.ProgressPercentage + "%\n" +
                "当前速度: " + currentSpeed.ToString("F2") + "KB/s\n" +
                "大速度: " + maxSpeed.ToString("F2") + "KB/s\n" +
                "用时: " + (useTime.Minutes * 60 + useTime.Seconds) + "s";

if (e.BytesReceived == e.TotalBytesToReceive)
            {
                webClint.DownloadProgressChanged -= webClint_DownloadProgressChanged;
                webClint.Dispose();
                Response.Write("任务完成!");
                webClint.Dispose();
                //if (MessageBox.Show("是否关闭当前程序而立即安装?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                //{
                //    xiazai();
                //}
            }

}

winform 下载的更多相关文章

  1. winform下载网页源码

    public partial class Form1 : Form{public Form1(){InitializeComponent();} private void button1_Click( ...

  2. C# Winform下载文件并显示进度条

    private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...

  3. winform下载网页代码

    1:webClient client=new WebClient(); client.Downloadstring(地址) client.Downloadfile(地址,保存路径) 2:后台线程dow ...

  4. Winform 下载服务器安装包并安装

    代码中包含了检测本地安装盘符代码 一,定义下载委托事件(用于实现前台进度条更新和下载完成后事件的回调): private delegate void Action(); private string ...

  5. Winform下载文件并显示进度条

    本来是要研究怎样判断下载完成,结果找到这个方法,可以在这个方法完成之后提示下载完成. 代码如下: using System; using System.Collections.Generic; usi ...

  6. winform 下载文件显示进度和百分比

    /// <summary> /// 下载完成 /// </summary> private void DownloadFileCompleted() { IsComlate = ...

  7. 个人收藏--未整理—C# 上传下载文件

    Winform下载文件 /// <summary> /// 下载文件 /// </summary> /// <param name="URL"> ...

  8. ssential Diagram for Windows FormsC#/winForm类似visio的拓扑图节点连线控件免费下载

    Essential Diagram for Windows Forms是一款可扩展的.高性能的.NET平台下的拓扑图控件,可用于开发像Microsoft Visio一样的交互式地绘图和图解应用程序,在 ...

  9. SNF开发平台WinForm之六-上传下载组件使用-SNF快速开发平台3.3-Spring.Net.Framework

    6.1运行效果: 6.2开发实现: 1.先在要使用的项目进行引用,SNF.WinForm.Attachments.dll文件. 2.在工具箱内新建选项卡->选择项,浏览找到文件SNF.WinFo ...

随机推荐

  1. numpy linalg模块

    # 线性代数# numpy.linalg模块包含线性代数的函数.使用这个模块,可以计算逆矩阵.求特征值.解线性方程组以及求解行列式等. import numpy as np # 1. 计算逆矩阵# 创 ...

  2. Java继承和组合

    为了保证父类有良好的封装性,不会被子类随意修改,设计父类通常应该遵循以下规则: 1.尽量隐藏父类的内部数据,尽量把父类的所有成员变量设置为 private 访问类型,不要让子类直接访问父类的成员变量: ...

  3. Docker Swarm 介绍 or 工作原理

    Docker Swarm 介绍 Swarm 简介 Swarm是Docker公司自研发的容器集群管理系统,Swarm在早期是作为一个独立服务存在,在Docker Engine v1.12中集成了Swar ...

  4. Celery 实现异步任务-one

    celery异步任务: 环境准备 安装celery ,django-celery. 就是一个专注于实时处理和任务调度的分布式队列. 可以异步执行的任务交给后台处理,以防网络阻塞,减小响应时间 cele ...

  5. JavaScript-DOM(3)

    事件处理 事件类型 <body> <!--方式1:直接带html代码中嵌入js代码--> <button onclick="console.log('事件1') ...

  6. ubuntu 关闭 笔记本键盘背景灯

    /etc/rc.local 加入 ' > /sys/class/leds/tpacpi::kbd_backlight/brightness

  7. RNA velocity | RNA速率

    单细胞转录组确实是利器,但我们大多只利用了表达的信息,而从reads到表达之间的信息完全被我们忽略了. 最近nature发了一篇单细胞方法类文章,讲得就是如何利用RNA velocity来做细胞发育路 ...

  8. day34 GIL锁 线程队列 线程池

    一.Gil锁(Global Interpreter Lock) python全局解释器锁,有了这个锁的存在,python解释器在同一时间内只能让一个进程中的一个线程去执行,这样python的多线程就无 ...

  9. poj 3254Corn Fields (入门状压dp)

    Farmer John has purchased a lush ≤ M ≤ ; ≤ N ≤ ) square parcels. He wants to grow some yummy corn fo ...

  10. 服务调用restful或feign负载均衡ribbon