C# 带进度条的文件下载
1 private long fileLength; 2 3 private long downLength;//已经下载文件大小,外面想用就改成公共属性 4 5 private static bool stopDown; 6 7 public HttpDownLoad() 8 9 { 10 11 fileLength = 0; 12 13 downLength = 0; 14 15 stopDown = false; 16 17 // 18 19 // TODO: 在此处添加构造函数逻辑 20 21 // 22 23 } 24 25 26 27 /// <summary> 28 29 /// 文件下载 30 31 /// </summary> 32 33 /// <param name= "url "> 连接 </param> 34 35 /// <param name= "fileName "> 本地保存文件名 </param> 36 37 /// <param name= "progressBar "> 进度条 </param> 38 39 public void httpDownFile(string url, string fileName, ProgressBar progressBar) 40 41 { 42 43 Label lable = new Label(); 44 45 httpDownFile(url, fileName, progressBar, lable); 46 47 lable.Dispose(); 48 49 } 50 51 52 53 /// <summary> 54 55 /// 文件下载 56 57 /// </summary> 58 59 /// <param name= "url "> 连接 </param> 60 61 /// <param name= "fileName "> 本地保存文件名 </param> 62 63 /// <param name= "progressBar "> 进度条 </param> 64 65 /// <param name= "label "> 返回已经下载的百分比 </param> 66 67 public string httpDownFile(string url, string fileName, ProgressBar progressBar, Label label) 68 69 { 70 71 string strState = "No"; 72 73 stopDown = false; 74 75 Stream str = null, fs = null; 76 77 try 78 79 { 80 81 //获取下载文件长度 82 83 fileLength = getDownLength(url); 84 85 downLength = 0; 86 87 if (fileLength > 0) 88 89 { 90 91 WebClient DownFile = new WebClient(); 92 93 str = DownFile.OpenRead(url); 94 95 //判断并建立文件 96 97 if (createFile(fileName)) 98 99 { 100 101 byte[] mbyte = new byte[1024]; 102 103 int readL = str.Read(mbyte, 0, 1024); 104 105 fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); 106 107 //读取流 108 109 while (readL != 0) 110 111 { 112 113 if (stopDown) 114 115 break; 116 117 downLength += readL;//已经下载大小 118 119 fs.Write(mbyte, 0, readL);//写文件 120 121 readL = str.Read(mbyte, 0, 1024);//读流 122 123 progressBar.Value = (int)(downLength * 100 / fileLength); 124 125 label.Text = progressBar.Value.ToString() + "% "; 126 127 Application.DoEvents(); 128 129 strState = "OK"; 130 131 } 132 133 str.Close(); 134 135 fs.Close(); 136 137 } 138 139 } 140 141 } 142 143 catch (Exception) 144 145 { 146 147 if (str != null) 148 149 str.Close(); 150 151 if (fs != null) 152 153 fs.Close(); 154 155 } 156 157 return strState; 158 159 } 160 161 162 163 /// <summary> 164 165 /// 文件下载 166 167 /// </summary> 168 169 /// <param name= "url "> 连接 </param> 170 171 /// <param name= "fileName "> 本地保存文件名 </param> 172 173 public void httpDownFile(string url, string fileName) 174 175 { 176 177 try 178 179 { 180 181 WebClient DownFile = new WebClient(); 182 183 DownFile.DownloadFile(url, fileName); 184 185 } 186 187 catch (Exception) 188 189 { 190 191 //MessageBox.Show(ex.Message); 192 193 } 194 195 } 196 197 198 199 /// <summary> 200 201 /// 获取下载文件大小 202 203 /// </summary> 204 205 /// <param name= "url "> 连接 </param> 206 207 /// <returns> 文件长度 </returns> 208 209 private long getDownLength(string url) 210 211 { 212 213 try 214 215 { 216 217 WebRequest wrq = WebRequest.Create(url); 218 219 WebResponse wrp = (WebResponse)wrq.GetResponse(); 220 221 wrp.Close(); 222 223 return wrp.ContentLength; 224 225 } 226 227 catch (Exception) 228 229 { 230 231 //MessageBox.Show(ex.Message); 232 233 return 0; 234 235 } 236 237 } 238 239 240 241 /// <summary> 242 243 /// 建立文件(文件如已经存在,删除重建) 244 245 /// </summary> 246 247 /// <param name= "fileName "> 文件全名(包括保存目录) </param> 248 249 /// <returns> </returns> 250 251 private bool createFile(string fileName) 252 253 { 254 255 try 256 257 { 258 259 if (File.Exists(fileName)) 260 261 { 262 263 File.Delete(fileName); 264 265 } 266 267 Stream s = File.Create(fileName); 268 269 s.Close(); 270 271 return true; 272 273 } 274 275 catch (Exception) 276 277 { 278 279 //MessageBox.Show(ex.Message); 280 281 return false; 282 283 } 284 285 } 286 287 288 289 public void downClose() 290 291 { 292 293 stopDown = true; 294 }