c# .net 网上文件下载demo

本文介绍了一个简单的C#程序,用于从指定URL下载图片资源到本地文件系统。通过使用System.Net命名空间下的WebRequest和WebResponse类,该程序实现了网络请求并读取响应流,最终将图片保存为本地文件。
很简单的一个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 


转载于:https://www.cnblogs.com/vivyli/archive/2010/02/05/1664077.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值