加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#利用WebClient实现两种方式下载文件

发布时间:2020-12-15 06:10:17 所属栏目:百科 来源:网络整理
导读:最近整理了WebClient 两种方式下载文件 ,留作以后查询。 第一种 string URLAddress = @"http://xiazai.jb51.net";string receivePath=@"C:";client.DownloadFile(URLAddress,receivePath + System.IO.Path.GetFileName(URLAddress)); 就OK了。 第二种 Stre

最近整理了WebClient 两种方式下载文件 ,留作以后查询。

第一种

string URLAddress = @"http://xiazai.aspzz.cn";

string receivePath=@"C:";

client.DownloadFile(URLAddress,receivePath + System.IO.Path.GetFileName(URLAddress));

 就OK了。

第二种

 Stream str = client.OpenRead(URLAddress);
 StreamReader reader = new StreamReader(str);
 byte[] mbyte = new byte[1000000];
 int allmybyte = (int)mbyte.Length;
 int startmbyte = 0;

 while (allmybyte > 0)
 {

 int m = str.Read(mbyte,startmbyte,allmybyte);
 if (m == 0)
  break;

 startmbyte += m;
 allmybyte -= m;
 }

 reader.Dispose();
 str.Dispose();

 string path = receivePath + System.IO.Path.GetFileName(URLAddress);
 FileStream fstr = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write);
 fstr.Write(mbyte,startmbyte);
 fstr.Flush();
 fstr.Close(); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读