以下是该下载类
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Windows.Forms; using System.Web; using System.Net; // //luyugao.com // namespace 自动升级 { class HttpDF { private long fileLength; private long downLength;//已经下载文件大小,外面想用就改成公共属性 private static bool stopDown; public string HttpDownLoad() { fileLength = 0; downLength = 0; stopDown = false; // // TODO: 在此处添加构造函数逻辑 // return ""; } /// <summary> /// 文件下载 /// </summary> /// <param name= "url "> 连接 </param> /// <param name= "fileName "> 本地保存文件名 </param> /// <param name= "progressBar "> 进度条 </param> public void httpDownFile(string url, string fileName, ProgressBar progressBar) { Label lable = new Label(); httpDownFile(url, fileName, progressBar, lable); lable.Dispose(); } /// <summary> /// 文件下载 /// </summary> /// <param name= "url "> 连接 </param> /// <param name= "fileName "> 本地保存文件名 </param> /// <param name= "progressBar "> 进度条 </param> /// <param name= "label "> 返回已经下载的百分比 </param> public string httpDownFile(string url, string fileName, ProgressBar progressBar, Label label) { string strState = "No"; stopDown = false; Stream str = null, fs = null; try { //获取下载文件长度 fileLength = getDownLength(url); downLength = 0; if (fileLength > 0) { WebClient DownFile = new WebClient(); str = DownFile.OpenRead(url); //判断并建立文件 if (createFile(fileName)) { byte[] mbyte = new byte[1024]; int readL = str.Read(mbyte, 0, 1024); fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); //读取流 while (readL != 0) { if (stopDown) break; downLength += readL;//已经下载大小 fs.Write(mbyte, 0, readL);//写文件 readL = str.Read(mbyte, 0, 1024);//读流 //progressBar.Value = (int)(downLength * 100 / fileLength); //label.Text = progressBar.Value.ToString() + "% "; progressBar.Value = (int)(downLength * 100 / fileLength); double p = (double)(downLength * 100) / (double)fileLength; p = Math.Round(Convert.ToDouble(p), 1, MidpointRounding.AwayFromZero); label.Text = p.ToString() + "% "; Application.DoEvents(); strState = "OK"; } str.Close(); fs.Close(); } } } catch (Exception) { if (str != null) str.Close(); if (fs != null) fs.Close(); } return strState; } /// <summary> /// 文件下载 /// </summary> /// <param name= "url "> 连接 </param> /// <param name= "fileName "> 本地保存文件名 </param> public void httpDownFile(string url, string fileName) { try { WebClient DownFile = new WebClient(); DownFile.DownloadFile(url, fileName); } catch { } } /// <summary> /// 获取下载文件大小 /// </summary> /// <param name= "url "> 连接 </param> /// <returns> 文件长度 </returns> private long getDownLength(string url) { try { WebRequest wrq = WebRequest.Create(url); WebResponse wrp = (WebResponse)wrq.GetResponse(); wrp.Close(); return wrp.ContentLength; } catch { return 0; } } /// <summary> /// 建立文件(文件如已经存在,删除重建) /// </summary> /// <param name= "fileName "> 文件全名(包括保存目录) </param> /// <returns> </returns> private bool createFile(string fileName) { try { if (!Directory.Exists(Path.GetDirectoryName(fileName)))//判断目标文件的文件夹是否存在 Directory.CreateDirectory(Path.GetDirectoryName(fileName));//循环创建 if (File.Exists(fileName)) File.Delete(fileName); Stream s = File.Create(fileName); s.Close(); return true; } catch { return false; } } /// <summary> /// 停止下载 /// </summary> public void downClose() { stopDown = true; } } }
来看怎么使用,以下代码可以放在循环体重进行循环下载
HttpDF df = new HttpDF(); string s = df.httpDownFile(网页文件地址, 本地保存路径, ProgressBar, Label); df.downClose();
其中后两个参数是用来显示下载进度的,试试看,在软件升级的时候肯定用得到~!