作者:SureChueng | 来源:互联网 | 2023-10-12 08:37
C#利用WebClient两种方式下载文件关键词:c#,webclientWebClientclientnewWebClient();第一种stringURLAddress@;s
C#利用WebClient两种方式下载文件
关键词:c#,webclient
WebClient client = new WebClient();
第一种
string URLAddress = @"http://files.gaodaima.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();
来源编程笔记《C#利用WebClient两种方式下载文件》http://www.gaodaima.com/68557.html