zoukankan      html  css  js  c++  java
  • C# winform实现下载带进度条

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using MetroFramework.Forms;
    11 
    12 namespace KMS_Starter
    13 {
    14     public partial class Form2 : MetroForm
    15     {
    16         public Form2()
    17         {
    18             InitializeComponent();
    19         }
    20 
    21         private void Form2_Load(object sender, EventArgs e)
    22         {
    23 
    24         }
    25 
    26         private void metroButton1_Click(object sender, EventArgs e)
    27         {
    28             DownloadFile("http://localhost:1928/WebServer/downloader/123.rar", @"C:123.rar", metroProgressBar1, label2);
    29         }
    30         /// <summary>        
    31         /// c#,.net 下载文件        
    32         /// </summary>        
    33         /// <param name="URL">下载文件地址</param>       
    34         /// 
    35         /// <param name="Filename">下载后的存放地址</param>        
    36         /// <param name="Prog">用于显示的进度条</param>        
    37         /// 
    38         public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)
    39         {
    40             float percent = 0;
    41             try
    42             {
    43                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
    44                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
    45                 long totalBytes = myrp.ContentLength;
    46                 if (prog != null)
    47                 {
    48                     prog.Maximum = (int)totalBytes;
    49                 }
    50                 System.IO.Stream st = myrp.GetResponseStream();
    51                 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
    52                 long totalDownloadedByte = 0;
    53                 byte[] by = new byte[1024];
    54                 int osize = st.Read(by, 0, (int)by.Length);
    55                 while (osize > 0)
    56                 {
    57                     totalDownloadedByte = osize + totalDownloadedByte;
    58                     System.Windows.Forms.Application.DoEvents();
    59                     so.Write(by, 0, osize);
    60                     if (prog != null)
    61                     {
    62                         prog.Value = (int)totalDownloadedByte;
    63                     }
    64                     osize = st.Read(by, 0, (int)by.Length);
    65 
    66                     percent = (float)totalDownloadedByte / (float)totalBytes * 100;
    67                     label2.Text = "当前补丁下载进度" + percent.ToString() + "%";
    68                     System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
    69                 }
    70                 so.Close();
    71                 st.Close();
    72             }
    73             catch (System.Exception)
    74             {
    75                 throw;
    76             }
    77         }
    78     }
    79 }
  • 相关阅读:
    读书笔记 之《Thinking in Java》(对象、集合、异常)
    ArrayList 和 LinkedList的执行效率比较
    Hybris CronJob.
    C# 中的treeview绑定数据库(递归算法)
    identity_insert---实验性插入大批量数据和分页存储过程
    SQL Server 存储过程
    PL/SQL 在64位机上不能使用的问题解决
    登陆Oracle11g的企业管理器
    SQL在oracle和SQLserver将查询结果创建为新表的不同之处
    介绍一下内联、左联、右联
  • 原文地址:https://www.cnblogs.com/hack747/p/14341664.html
Copyright © 2011-2022 走看看