C# Winform下载文件并显示进度条的实现代码
作者:mdxy-dxy 时间:2022-11-26 04:39:02
方法一:
效果如下图所示:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinShowDown
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void btnDown_Click(object sender, EventArgs e)
{
DownloadFile("http://localhost:1928/WebServer/downloader/123.rar", @"C:\123.rar", progressBar1, label1);
}
/// <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;
label1.Text = "当前补丁下载进度" + percent.ToString() + "%";
System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
}
so.Close();
st.Close();
}
catch (System.Exception)
{
throw;
}
}
}
}
实现方法二:
WinForm下载文件并显示下载进度示例
/// <summary>
/// 显示进度
/// </summary>
/// <param name="val"></param>
private void ProgressBar_Value(int val)
{
progressBar1.Value = val;
label1.Text = val.ToString() + "%";
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="savefile"></param>
/// <param name="downloadProgressChanged"></param>
/// <param name="downloadFileCompleted"></param>
private void DownloadFile(string url, string savefile, Action<int> downloadProgressChanged, Action downloadFileCompleted)
{
WebClient client = new WebClient();
if (downloadProgressChanged != null)
{
client.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
this.Invoke(downloadProgressChanged, e.ProgressPercentage);
};
}
if (downloadFileCompleted != null)
{
client.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e)
{
this.Invoke(downloadFileCompleted);
};
}
client.DownloadFileAsync(new Uri(url), savefile);
}
delegate void Action(); //.NET Framework 2.0得自定义委托Action
/// <summary>
/// 点击下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DownloadFile("http://xiazai.jb51.net/update.zip", @"F:update.zip", ProgressBar_Value, null);
}
标签:Winform,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信公众号支付(二)实现统一下单接口
2023-01-12 21:29:18
Java中反射动态 代理接口的详解及实例
2023-11-28 05:29:36
![](https://img.aspxhome.com/file/2023/6/60266_0s.png)
android 实现控件左右或上下抖动教程
2023-03-19 17:02:07
解析Spring事件发布与监听机制
2022-09-01 09:52:19
![](https://img.aspxhome.com/file/2023/6/88586_0s.jpg)
IDEA设置Tab选项卡快速的操作
2022-07-15 18:20:31
![](https://img.aspxhome.com/file/2023/6/81196_0s.jpg)
C#事件订阅发布实现原理详解
2022-10-15 20:36:35
![](https://img.aspxhome.com/file/2023/1/88341_0s.png)
详解Java获取环境变量及系统属性的方法
2021-08-23 16:58:03
使用Java和ffmpeg把音频和视频合成视频的操作方法
2022-08-22 07:30:20
MyBatis动态sql查询及多参数查询方式
2023-05-30 12:20:12
C# 给PPT中的图表添加趋势线的方法
2022-02-27 10:18:40
![](https://img.aspxhome.com/file/2023/2/119422_0s.png)
java反射使用示例分享
2023-07-02 20:18:59
浅析SpringBoot2底层注解@Conditional@ImportResource
2023-08-01 23:35:51
![](https://img.aspxhome.com/file/2023/3/61793_0s.png)
C# 9 新特性——record的相关总结
2022-01-25 07:33:35
![](https://img.aspxhome.com/file/2023/5/116655_0s.png)
RxJava2.x实现定时器的实例代码
2023-08-06 17:41:01
Android 实现双击退出的功能
2021-06-16 09:08:45
Spring Boot创建非可执行jar包的实例教程
2022-10-05 12:47:35
Java Web实现自动登陆功能
2023-09-06 04:17:53
![](https://img.aspxhome.com/file/2023/3/103853_0s.jpg)
基于Mock测试Spring MVC接口过程解析
2023-11-27 12:04:30
如何实现Spring Event(异步事件)
2023-08-23 05:06:47
关于Flyweight模式应用实践的相关介绍
2021-07-28 21:50:34