程序代码:
// //哈,谢谢你,这个程序能用,我用的是VS2008,改了下。 // using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using namespace FileDownLoad { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private WebClient webDownLoad = new WebClient(); private void Form1_Load(object sender, EventArgs e) { this.localPathTxt.Text = @"F:\"; this.msgLab.Text = "准备就绪"; } private void okBtn_Click(object sender, EventArgs e) { this.progressBar1.Value = 0; if (this.urlTxt.Text == string.Empty) { return; } string filePath = this.localPathTxt.Text + "\\" + this.fileNameTxt.Text; webDownLoad.DownloadFileCompleted += new AsyncCompletedEventHandler(webDownLoad_DownloadFileCompleted); webDownLoad.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webDownLoad_DownloadProgressChanged); webDownLoad.DownloadFileAsync(new Uri(this.urlTxt.Text.Trim()), filePath); } private void openBtn_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.ShowNewFolderButton = true; DialogResult dr = fbd.ShowDialog(); if (dr == DialogResult.OK) { this.localPathTxt.Text = fbd.SelectedPath; } } private void urlTxt_TextChanged(object sender, EventArgs e) { string url = this.urlTxt.Text.Trim(); if (url != string.Empty) { int index = url.LastIndexOf('/'); this.fileNameTxt.Text = url.Substring(index + 1, url.Length - index - 1); } } private void cancelBtn_Click(object sender, EventArgs e) { webDownLoad.CancelAsync(); this.msgLab.Text = "下载取消"; this.progressBar1.Value = 0; } private void webDownLoad_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { msgLab.Text = string.Format("{0}%",e.ProgressPercentage.ToString("N2")); this.progressBar1.Value = e.ProgressPercentage; } private void webDownLoad_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { msgLab.Text = "下载取消"; this.progressBar1.Value = 0; } else { msgLab.Text = "下载完成"; } } } }
唯实惟新 至诚致志