![]() |
General Programming »
Internet / Network »
Internet
Intermediate
Easy-to-use resumable .NET file downloaderBy Phil CrosbyA simple, easy-to-use, resumable .NET file downloader. |
C#, Windows, .NET 1.0, .NET 1.1, .NET 2.0, Mono, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This is a simple-to-use file downloader class for use in .NET projects. The implementation is small, and the API is clean. It supports resuming, and has descriptive error exceptions.
This is based off of John Batte's initial CodeProject implementation, which worked well. I used his code for InstallPad, and felt it necessary to make a few improvements and share them.
Using the downloader is very simple. You can call it synchronously or asynchronously. Calling it synchronously will download the entire file before progressing to the next line of the method. Calling it asynchronously will let the execution of your method proceed while the downloader works in the background. You can listen to the progress of the download by subscribing to its events. Here is a simple sample which downloads FireFox:
static void Main(string[] args)
{
FileDownloader downloader = new FileDownloader();
// Listen for when the download completes.
downloader.DownloadComplete +=
new EventHandler(downloader_DownloadedComplete);
// Listen as each new packet of data comes in.
downloader.ProgressChanged +=
new DownloadProgressHandler(downloader_ProgressChanged);
// Download synchronously
downloader.Download("http://download.mozilla.org/" +
"?product=firefox-1.5.0.4&os=win&lang=en-US");
}
static void downloader_ProgressChanged(object sender, DownloadEventArgs e)
{
// DownloadEventArgs has lots of progress information
Console.WriteLine("Progress " + e.PercentDone);
}
static void downloader_DownloadedComplete(object sender, EventArgs e)
{
Console.WriteLine("Download complete.");
}
You can begin a download by using one of the following methods:
Download(string url)
Download(string url, string destFolder)
Download(List<String> urlList)
Download(List<String> urlList, string destFolder) There are asynchronous versions of each:
AsyncDownload(string url)
AsyncDownload(string url, string destFolder)
AsyncDownload(List<String> urlList)
AsyncDownload(List<String> urlList, string destFolder) And you can cancel a download anytime with the Cancel() method.
This code also supports using a proxy. You can build an IWebProxy object and assign it to the downloader through the downloader's Proxy property.
Anyone can contribute:
(Yes, that's five slashes.)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 25 Oct 2006 Editor: Smitha Vijayan |
Copyright 2006 by Phil Crosby Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |