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.