源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-08-31 13:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#利用WebClient实现两种方式下载文件
最近整理了WebClient 两种方式下载文件 ,留作以后查询。 [b]第一种[/b]
string URLAddress = @"http://xiazai.jb51.net";

string receivePath=@"C:\";

client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
 就OK了。 [b]第二种[/b]
 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, 0, startmbyte);
 fstr.Flush();
 fstr.Close(); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部