首页 技术 正文
技术 2022年11月15日
0 收藏 685 点赞 2,504 浏览 1579 个字
      private void button1_Click(object sender, EventArgs e)
{
doDownload(textBox1.Text.Trim());
} private DateTime StartTime;
private void doDownload(string url,string fileName="")
{ label1.Text = "正在下载:" + url;//label框提示下载文件
if (fileName.Length==0)
{
fileName = url.Substring(url.LastIndexOf("/") + 1);
}
StartTime = DateTime.Now;
progressBar1.Value = 0;//初始化单个文件下载条
WebClient ws = new WebClient();
ws.DownloadProgressChanged += OnDownloadProgressChanged;
//绑定下载事件,以便于显示当前进度
ws.DownloadFileCompleted += OnDownloadFileCompleted;
//绑定下载完成事件,以便于计算总进度
ws.DownloadFileAsync(new Uri(url), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
//调用DownloadFileAsync方法下载文件
//DownloadFileAsync有2个重载,另一个我没搞明白最后一个参数该传什么,有哪位朋友知道的,请留言告诉我谢谢
//upapp是我一个实体类,UpdateURL存放了下载的地址(值为http://www.harde.com.cn/SoftUpdate/)
//Path.Combine()是一个用来连接地址的方法,我将在另一日志中详细对其介绍
}
/// 下载进程变更事件
private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//在网上看到有朋友这么来控制进度条,我觉得麻烦,毕竟有省事的为什么我要麻烦一番……
//this.SetProcessBar(e.ProgressPercentage, (int)((nDownloadedTotal + e.BytesReceived) * 100 / total));
progressBar1.Value = e.ProgressPercentage;
var s = (DateTime.Now - StartTime).TotalSeconds;
var sd = ReadableFilesize(e.BytesReceived/s);
label1.Text = "下载速度"+sd+"/秒"+",已下载" + ReadableFilesize(e.BytesReceived) + "/总计" + ReadableFilesize(e.TotalBytesToReceive) ;//一个label框,用来显示当前下载的数据
}
/// 下载进程变更事件
private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("complied!");
}
private string ReadableFilesize(double size)
{
String[] units = { "B", "KB", "MB", "GB", "TB", "PB" };
double mod = 1024.0;
int i = 0;
while (size >= mod)
{
size /= mod;
i++;}
return Math.Round(size) + units[i];
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860