C # uses WebClient to implement two ways of downloading file code details

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.