Client file download based on WinForm, WPF, etc.

Source: Internet
Author: User

Sometimes, we write some client applications in C # that need to download some resources, tablets, DLLs, configuration files, etc. from the server. Here's a look at how to download files in WinForm and WPF.

Most of our resources are on our own website, or download resources from other sites, we need to give the client a URL, first give the code:

 // <summary>      ///Download File/// </summary>      /// <param name= "URL" >Download file Address</param>      /// <param name= "Filename" >the storage address after download</param>       Public voidDownloadFile (stringUrlstringfilename) {         Try{System.Net.HttpWebRequest Myrq=(System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URL); System.Net.HttpWebResponse MYRP=(System.Net.HttpWebResponse) myrq.getresponse (); LongTotalBytes =MYRP.            ContentLength; System.IO.Stream St=MYRP.            GetResponseStream (); System.IO.Stream So=NewSystem.IO.FileStream (filename, System.IO.FileMode.Create); LongTotaldownloadedbyte =0; byte[] by =New byte[1024x768]; intOsize = St. Read (By,0, (int) by.            Length);  while(Osize >0) {Totaldownloadedbyte= Osize +Totaldownloadedbyte; So. Write (By,0, osize); Osize= St. Read (By,0, (int) by.            Length); } so.            Close (); St.         Close (); }         Catch(System.Exception) {Throw; }      }

This method altogether two parameters, the first network URL, the second is a local storage location, such as D:\Download\, can be filled according to their own needs. Here's how to call it:

         New Downloader ();         Down. DownloadFile ("http://www.***.com/20140124.jpg"@ "d:\1.jpg ");

With this method, we can grab the Web content, through regular matching, to download the entire page of the picture, you can study it.

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.