Download a file for a given URL Using WebClient class in c#

Today i want to discuss an article how to Download a file with a given Uniform Resource Locator (URL) In which scenario it will be useful and how to use this Method. How to check Network Connection available through Coding ? I will discuss in detail in this Article with Code snippets.

Today i want to discuss an article how to Download a file with a given Uniform Resource Locator (URL) In which scneario it will be Useful i will discuss in detail in this Article with Code snippets.

Scenario :

I am working a Project in Windows Application that Windows Application will contain data downloaded from a Uniform resource locator that is Front end (Web UI part) why i am doing is because for some rich look. This UI part will give some readable information whenever the Application goes offline(i.e. Network disconnected) I do n't want to display a Page 404 found page or any Network Connection Failure page i need to show the UI part as it is because i do n't want to interrupt the working application for the end user . For that i will download all the Files from the URL to my Local Computer (when Network connection is available) and i will check in a timely manner whether the Network Connection is Available or not through some Piece of code whenever my check says that there is no Network Connection i had recent downloaded files in my local Computer . I will show the UI from my local computer Files which are downloaded when network is available.

To Download Files from the URL i.e. Possible with WebClient exists in the Name space Using System.Net and webclient Class is the one which is easily used to download files . The WebClient has two Methods called DownloadFile() and DownLoadFileAsync() . These both Methods will take two arguments
DownLoadFile(url of the file, path to Local disk) and DownLoadFileAsync() it will take same arguments as the DownloadFile Method but this method ( DownLoadFileAsync() ) asynchronously call the Method and will not block the Main thread.

And this Class has event Delegates ....

one is DownloadFileCompleted and another one is progresschanged

WebClient.DownLoadFileCompleted + =new AsyncCompletedEventHandler(completed)
WebClientDownloadProgressChanged + =new DownloadProgresschangedEventHandler(progresschanged).

now our Main Code of Downloading a File from the Uniform Resource Locator....

Using System.Net for C# Use Imports for vb getting Webclient class in your Code.

Using System.Net

///Because my windows Application will run several threads ...

WebClient.DownLoadFileAsync("", @"c://Desktop/DownloadedFiles");
catch(WebException ex)



WebClient.DownLoadFile("", @"c://Desktop/DownloadedFiles");
catch(WebException ex)


Code to check whether the Local user has Network Connection...


returns true when Connection is available / false when not available

You can download images from this Method.

Tested Fine and working in real environment

Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: