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 asp.net 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 system.net for vb getting Webclient class in your Code.
Using System.Net
///Because my windows Application will run several threads ...
try
{
WebClient.DownLoadFileAsync("http://myxyz.com/file.txt", @"c://Desktop/DownloadedFiles");
}
catch(WebException ex)
{
}
WebClient.DownloadFile
try
{
WebClient.DownLoadFile("http://myxyz.com/file.txt", @"c://Desktop/DownloadedFiles");
}
catch(WebException ex)
{
}
Code to check whether the Local user has Network Connection...
bool availconnection = SYSTEM.NET.NETWORKINFORMATION.NETWORKINTERFACE.GETISNETWORKAVAILABLE()
returns true when Connection is available / false when not available
You can download images from this Method.
Tested Fine and working in real environment