This article mainly introduces C # using WebClient two ways to download files, detailed introduction of two ways, very practical value, the need for friends can refer to.
Recently organized the WebClient two ways to download files for later query.
First Kind
String urladdress = @ "Http://xiazai.jb51.net"; string receivepath=@ "C: \"; client. DownloadFile (urladdress, Receivepath + System.IO.Path.GetFileName (urladdress));
It's OK.
The second Kind
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 C # using WebClient to achieve two ways to download the file code details, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!