Saturday, June 29, 2013

Downloading file from WebClient


Every one, once in a life time, downloaded something. Imagine the case when you have a webpage containing a lot of songs, and you need to download every song. You can either use the layman’s approach, that is right clicking on each and every link and saving file; or you can use the programmer's approach which is getting all HTML of the webpage, parsing it to get all the links and then downloading all the files simultaneously.
In this post however, we will see how to download a file from a website. Here we will download one of my favorite song Dilli Wali Girlfriend. For that I have created a simple console application. And just a 5 line of code served the purpose.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Net;
   5: using System.Text;
   6: using System.Threading.Tasks;
   7:  
   8: namespace DownloadingFile
   9: {
  10:     class Program
  11:     {
  12:         static void Main(string[] args)
  13:         {
  14:             var wc = new WebClient();
  15:             Console.WriteLine("Downloading...");
  16:             wc.DownloadFile(new Uri("http://link1.songspk.name/song1.php?songid=9870", 
  17:                                 UriKind.RelativeOrAbsolute), "H:\\DilliWaliGirlfriend.mp3");
  18:             Console.WriteLine("Completed!");
  19:             Console.ReadLine();
  20:         }
  21:     }
  22: }

To describe the process. I have created an object of the WebClient class wc. Calling the function DownloadFile. The function will take two parameters (however there are many overloads of it though) the uri of the link of the file and the exact location of the destination.
In the above code. I have created a URI from the link which I then passed to the DownloadFile function. And for the destination, I chose a location on my hard drive. Run the project and it will download the file specified in the link to the desired location.

Happy Reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top