服务器之家

服务器之家 > 正文

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

时间:2021-12-23 13:32     来源/作者:x4646

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

第一种

?
1
2
3
4
5
string URLAddress = @"https://tool.zzvips.com";
 
string receivePath=@"C:\";
 
client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));

 就OK了。

第二种

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/x4646/archive/2013/04/11/3014634.html

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部