您的位置:首页 > 编程语言 > C#

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

2013-04-11 15:51 155 查看
WebClient client = new WebClient();

第一种

string URLAddress = @"http://files.cnblogs.com/x4646/tree.zip";

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: