首页  >  文章  >  后端开发  >  C#利用WebClient实现两种方式下载文件代码详情

C#利用WebClient实现两种方式下载文件代码详情

黄舟
黄舟原创
2017-03-07 11:41:062807浏览

本篇文章主要介绍了C#利用WebClient 两种方式下载文件,详细的介绍了两种方式,非常具有实用价值,需要的朋友可以参考下。

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

第一种

string URLAddress = @"http://xiazai.jb51.net";

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, 0, startmbyte);
 fstr.Flush();
 fstr.Close();

 以上就是C#利用WebClient实现两种方式下载文件代码详情的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn