最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#利用WebClient实现两种方式下载文件
时间:2022-06-25 07:54:58 编辑:袖梨 来源:一聚教程网
最近整理了WebClient 两种方式下载文件 ,留作以后查询。
第一种
代码如下 | 复制代码 |
stringURLAddress =@"http://xiazai.jb51.net";
stringreceivePath=@"C:";
client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress)); |
就OK了。
第二种
代码如下 | 复制代码 |
Stream str = client.OpenRead(URLAddress); StreamReader reader =newStreamReader(str); byte[] mbyte =newbyte[1000000]; intallmybyte = (int)mbyte.Length; intstartmbyte = 0;
while(allmybyte > 0) {
intm = str.Read(mbyte, startmbyte, allmybyte); if(m == 0) break;
startmbyte += m; allmybyte -= m; }
reader.Dispose(); str.Dispose();
stringpath = receivePath + System.IO.Path.GetFileName(URLAddress); FileStream fstr =newFileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, startmbyte); fstr.Flush(); fstr.Close(); |
相关文章
- Solana因伊朗-以色列动荡遭受重创——SOL价格会持续下跌吗? 06-16
- 《雷霆战机集结》Boss模式关卡刷取建议 06-16
- 《世界启元》S6赛季芝诺周瑜队搭配攻略 06-16
- 《世界启元》S6赛季新英雄达尔文技能分析及配队推荐 06-16
- 《纸嫁衣3鸳鸯债》全章节超详细攻略 06-16
- 杠杆、流动性与多头:无人预见的山寨币反弹格局 06-16