C#應用WebClient完成兩種方式下載文件。本站提示廣大學習愛好者:(C#應用WebClient完成兩種方式下載文件)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。