C# Download File Asynchronously from winform
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; namespace WindowsTreeViewDemo { /// <summary> /// 2011-06-25 /// 涂聚文 QQ天气预报 /// </summary> public partial class Form1 : Form { WebClient webClient = new WebClient(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadFileAsync(new Uri("http://weather.qq.com/inc/ss1.htm"), @"c:\201106021ss1.html"); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; this.label1.Text = "已完成:" + progressBar1.Value.ToString() + "%"; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Completed(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("Download completed!"); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { //取消上传 if (webClient != null) { webClient.CancelAsync(); } Close(); } } }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)