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 }
复制代码

 

posted @   hack747  阅读(478)  评论(1编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示