using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
WebClient webClient = new WebClient();
public Form1()
{
InitializeComponent();
this.textBox1.Text =
@"http://219.146.241.17/dlied1.qq.com/dnf/dltools/DNF_SEASON4_OB_V12.17_Full_B_XFDL_signed.exe?mkey=553a1f3122241ccf&f=188a&p=.exe";
}
private void button1_Click(object sender, EventArgs e)
{
if (webClient.IsBusy)//是否存在正在进行中的Web请求
{
webClient.CancelAsync();
}
//为webClient添加事件
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
//开始下载
webClient.DownloadFileAsync(new Uri(this.textBox1.Text), "aa.rar");
}
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.lbl_pro.Text = e.ProgressPercentage.ToString() + "%";
this.lbl_detail.Text = string.Format("正在下载文件,完成进度{0}/{1}(字节)"
, e.BytesReceived
, e.TotalBytesToReceive);
}
private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
MessageBox.Show("下载被取消!");
else
MessageBox.Show("下载完成!");
}
private void button2_Click(object sender, EventArgs e)
{
this.webClient.CancelAsync();
this.webClient.Dispose();
}
}
}
webclient下载文件
最新推荐文章于 2023-03-28 15:29:28 发布