运行效果:
项目结构图:
DownLoad.cs文件代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace DawnLoad 12 { 13 public partial class DownLoad : Form 14 { 15 public DownLoad() 16 { 17 InitializeComponent(); 18 } 19 20 //下载路径,文件保存路径 21 public static string DownLoadPath; 22 public static string StoragePath; 23 24 /// <summary> 25 /// 加载事件 26 /// </summary> 27 private void DownLoad_Load(object sender, EventArgs e) 28 { 29 30 } 31 32 /// <summary> 33 /// 下载事件 34 /// </summary> 35 /// <param name="sender"></param> 36 /// <param name="e"></param> 37 private void button1_Click_1(object sender, EventArgs e) 38 { 39 //给定下载路径 40 DownLoadPath = this.txt_DowanLoadPath.Text.Trim(); 41 42 //判断所输入的路径是不是为空 43 Command.IsNotEmpty.txtIsNotEmpty(DownLoadPath, StoragePath); 44 45 //格式化文件保存路径,使的保存的路径后面所带的文件名,跟下载时候所带有的文件名称一致 46 string newStoragePath = Command.FomatPath.FomatStoragePath(DownLoadPath, StoragePath); 47 48 //下载开始下载 49 Command.DownLoad.DownloadFile(DownLoadPath, newStoragePath, progressBar1, lbl_JD); 50 } 51 52 /// <summary> 53 /// 浏览事件,选择保存路径 54 /// </summary> 55 /// <param name="sender"></param> 56 /// <param name="e"></param> 57 private void button2_Click(object sender, EventArgs e) 58 { 59 FolderBrowserDialog folderDlg = new FolderBrowserDialog(); 60 folderDlg.ShowDialog(); 61 folderDlg.Description = "请选择保存文件的文件夹!"; 62 if (folderDlg.SelectedPath == "") 63 { 64 return; 65 } 66 else 67 { 68 StoragePath = folderDlg.SelectedPath; 69 this.txt_StoragePathPath.Text = StoragePath; 70 return; 71 } 72 } 73 } 74 }
Command文件夹下的DownLoad.cs代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace DawnLoad.Command 9 { 10 public static class DownLoad 11 { 12 /// <summary> 13 /// c#,.net 下载文件 14 /// </summary> 15 /// <param name="URL">下载文件地址</param> 16 /// <param name="Filename">下载后的存放地址</param> 17 /// <param name="Prog">用于显示的进度条</param> 18 /// <param name="label1">显示下载完成度</param> 19 public static void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) 20 { 21 float percent = 0; 22 try 23 { 24 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); 25 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); 26 long totalBytes = myrp.ContentLength; 27 if (prog != null) 28 { 29 prog.Maximum = (int)totalBytes; 30 } 31 System.IO.Stream st = myrp.GetResponseStream(); 32 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); 33 long totalDownloadedByte = 0; 34 byte[] by = new byte[1024 * 1024 * 1]; 35 int osize = st.Read(by, 0, (int)by.Length); 36 while (osize > 0) 37 { 38 totalDownloadedByte = osize + totalDownloadedByte; 39 System.Windows.Forms.Application.DoEvents(); 40 so.Write(by, 0, osize); 41 if (prog != null) 42 { 43 prog.Value = (int)totalDownloadedByte; 44 } 45 osize = st.Read(by, 0, (int)by.Length); 46 47 percent = (float)totalDownloadedByte / (float)totalBytes * 100; 48 label1.Text = " 当前下载进度" + percent.ToString() + "%"; 49 System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 50 } 51 MessageBox.Show("下载完成!", "提示"); 52 so.Close(); 53 st.Close(); 54 } 55 catch (System.Exception ex) 56 { 57 MessageBox.Show(ex.Message, "提示"); 58 } 59 } 60 } 61 }
Command文件夹下的FomatPath.cs代码:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DawnLoad.Command 9 { 10 public static class FomatPath 11 { 12 /// <summary> 13 /// 格式化保存文件的路径 14 /// </summary> 15 /// <param name="DownLoadPath">下载文件的URL,用来截取文件名</param> 16 /// <param name="StoragePath">保存文件的原始路径</param> 17 /// <returns>格式化后的路径</returns> 18 public static string FomatStoragePath(string DownLoadPath, string StoragePath) 19 { 20 string newStoragePath = string.Empty; 21 22 string newDownLoadPath = Path.GetFileName(DownLoadPath); 23 24 newDownLoadPath = newDownLoadPath.Replace('&', ' '); 25 26 newStoragePath = StoragePath + "\" + newDownLoadPath; 27 28 return newStoragePath; 29 } 30 } 31 }
Command文件夹下的IsNotEmpty.cs代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace DawnLoad.Command 9 { 10 public static class IsNotEmpty 11 { 12 public static void txtIsNotEmpty(string DownLoadPath, string StoragePath) 13 { 14 if (DownLoadPath.Trim().Length == 0) 15 { 16 MessageBox.Show("下载路径文本框不能为空!", "提示"); 17 return; 18 } 19 20 if (StoragePath.Trim().Length == 0) 21 { 22 MessageBox.Show("保存路径文本框不能为空!", "提示"); 23 return; 24 } 25 } 26 } 27 }
源码:百度云,[下载工具]文件夹。