• winform 下载


    方法一:

    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();
                    //}
                }

            }

    如果您认为这篇文章还不错或者有所收获,您可以点击文章下面的【推荐】按钮精神支持,因为这种支持是我继续写作,分享的最大动力!
    声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
  • 相关阅读:
    #一点杂记
    《洛谷P3373 【模板】线段树 2》
    《Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020
    《牛客练习赛72C》
    《hdu2819》
    《hdu2818》
    《Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)》
    《51nod1237 最大公约数之和 V3》
    对输入的单词进行排序
    快速排序
  • 原文地址:https://www.cnblogs.com/gskstudy/p/3995332.html
Copyright © 2020-2023  润新知