首页 > 编程语言 >C# winform实现下载带进度条

C# winform实现下载带进度条

时间:2022-11-24 16:26:16浏览次数:37  
标签:totalDownloadedByte 进度条 C# osize System Forms using Net winform

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using MetroFramework.Forms;
 
 namespace KMS_Starter
 {
     public partial class Form2 : MetroForm
     {
         public Form2()
         {
             InitializeComponent();
         }
 
         private void Form2_Load(object sender, EventArgs e)
         {
 
         }
 
         private void metroButton1_Click(object sender, EventArgs e)
         {
             DownloadFile("http://localhost:1928/WebServer/downloader/123.rar", @"C:123.rar", metroProgressBar1, label2);
         }
         /// <summary>        
         /// c#,.net 下载文件        
         /// </summary>        
         /// <param name="URL">下载文件地址</param>       
         /// 
         /// <param name="Filename">下载后的存放地址</param>        
         /// <param name="Prog">用于显示的进度条</param>        
         /// 
         public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)
         {
             float percent = 0;
             try
             {
                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                 long totalBytes = myrp.ContentLength;
                 if (prog != null)
                 {
                     prog.Maximum = (int)totalBytes;
                 }
                 System.IO.Stream st = myrp.GetResponseStream();
                 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                 long totalDownloadedByte = 0;
                 byte[] by = new byte[1024];
                 int osize = st.Read(by, 0, (int)by.Length);
                 while (osize > 0)
                 {
                     totalDownloadedByte = osize + totalDownloadedByte;
                     System.Windows.Forms.Application.DoEvents();
                     so.Write(by, 0, osize);
                     if (prog != null)
                     {
                         prog.Value = (int)totalDownloadedByte;
                     }
                     osize = st.Read(by, 0, (int)by.Length);
 
                     percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                     label2.Text = "当前补丁下载进度" + percent.ToString() + "%";
                     System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
                 }
                 so.Close();
                 st.Close();
             }
             catch (System.Exception)
             {
                 throw;
             }
         }
     }
 }

 

标签:totalDownloadedByte,进度条,C#,osize,System,Forms,using,Net,winform
From: https://www.cnblogs.com/roak/p/16922231.html

相关文章

  • 001 ArcObjects SDK 简介
    1、什么是ArcObjectsSDK在网上搜索什么是ArcObjects,会搜到如下的定义。这个定义比较准确,也比较容易理解。2、什么是ArcEngine在网上搜索ArcEngine,一般会搜到以下定义。这......
  • C++中的Type Alias
    在C++中,我们通常使用typedef来实现typealias.比如:#include<cstdint>//Cstandardinttypedefuint32_tpoints_t;//points_tisaliasofuint32_ttypedefuin......
  • systemctl提示start docker.service: Unit not found. (一个伤心的故事)
    前因:很久之前做了一个项目,最近要在项目上更新,发现之前部署的K8S集群好几个节点掉线了,一顿操作下来发现这几台机器上的docker服务挂了。习惯性的喜欢用systemctl......
  • C++ 简易按键精灵制作
     简易按键精灵制作参考链接:https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-sendinput?redirectedfrom=MSDN、https://www.fluentcpp.com/20......
  • Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque 题解
    圆由于没有相交,之间的关系要么毫无关系,要么就是包含,所以能形成树。直接包含的就是父节点。如果只有一组,不分前半夜后半夜的话,那么舒适度就是每棵树的根节点(深度为0)面积......
  • 【转】WPF DataContext
    简介:    获取或设置元素参与数据绑定时的数据上下文。数据上下文是一种概念,允许元素从父元素继承有关用于绑定的数据源以及绑定的其他特征(如路径)的信息。此依赖......
  • Linux CentOS7.X-命令记录
    1、链接命令ln硬链接:相当于复制。ln源文件(路径 )   目标文件(路径)硬链接文件,只要修改了其中一个文件,另外一个文件也会同步修改,但是删除操作不......
  • PHP轻量级验证器 Particle\Validator
    Particle\Validator是一个小巧优雅的实用的PHP验证类库,提供了一个非常简洁的API。它无需依赖其他组件,提供友好的文档,并且有利于扩展。composerrequireparticle/validat......
  • vimrc
    "Allsystem-widedefaultsaresetin$VIMRUNTIME/debian.vimandsourcedby"thecallto:runtimeyoucanfindbelow.Ifyouwishtochangeanyofthose"sett......
  • Pig4Cloud之jasypt 配置文件加密
    简介Jasypt是一个Java简易加密库,用于加密配置文件中的敏感信息,如数据库密码。jasypt库与springboot集成,在实际开发中非常方便。引入Jasypt依赖<dependency><groupI......