c# .net 网上文件下载demo

很简单的一个demo,用于下载网站上的资源,代码如下

 1 using System;
 2 using System.IO;
 3 using System.Net;
 4 using System.Text.RegularExpressions;
 5 using System.Text;
 6 
 7 class MyApp
 8 {
 9     static void Main(string[] args)
10     {
11 
12         Stream reader = null;
13         FileStream stream = null;
14         try
15         {
16             WebRequest request = WebRequest.Create("http://www.cc98.org/uploadface/154294.jpg");
17             WebResponse response = request.GetResponse();
18             reader = response.GetResponseStream();
19             stream = File.Open("text.jpg", FileMode.OpenOrCreate, FileAccess.Write);
20             byte[] buf = new byte[512];
21             int cnt;
22             while ((cnt = reader.Read(buf, 0, buf.Length)) > 0)
23             {
24                 stream.Write(buf, 0, cnt);
25             }
26         }
27         catch (Exception e)
28         {
29             Console.WriteLine(e.Message);
30         }
31         finally
32         {
33             if (reader != null)
34                 reader.Close();
35             stream.Close();
36         }
37     }
38 }
39 


posted on 2010-02-05 02:12  vivy  阅读(272)  评论(0编辑  收藏  举报