Home >Backend Development >C#.Net Tutorial >C# uses WebClient to implement two ways to download file code details
This article mainly introduces the two methods of using WebClient to download files in C#. It introduces the two methods in detail. It is of great practical value. Friends in need can refer to it.
Recently sorted out the two ways of downloading files using WebClient and left them for future inquiry.
The first one
string URLAddress = @"http://xiazai.jb51.net"; string receivePath=@"C:\"; client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
is OK.
Second type
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();
The above is the content of C# using WebClient to implement two ways to download file code details. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!