Click here to Skip to main content
6,635,160 members and growing! (13,385 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Internet     Intermediate

Easy-to-use resumable .NET file downloader

By Phil Crosby

A simple, easy-to-use, resumable .NET file downloader.
C#, Windows, .NET 1.0, .NET 1.1, .NET 2.0, Mono, Visual Studio, Dev
Posted:10 Jun 2006
Updated:25 Oct 2006
Views:61,165
Bookmarked:175 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
31 votes for this article.
Popularity: 6.94 Rating: 4.65 out of 5
1 vote, 3.2%
1

2
1 vote, 3.2%
3
6 votes, 19.4%
4
23 votes, 74.2%
5

Introduction

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.

Background

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.

  • Greatly simplified API
  • Supports ftp:// and file:// URLs
  • Informative progress information
  • Obtains file name from the server, which allows downloading from URLs with query strings, e.g., http://server.com/?file
  • Removal of WaitHandles
  • Distributed as a .cs file instead of an unfortunate EXE installer
  • Download from a list of URLs, in case one of the URLs fails
  • Proxy support
  • Descriptive exceptions - you'll get something like "Error saving file (file)" instead of "Invalid characters in path", with the original exception accessible as an inner exception
  • Compiles without warnings in Visual Studio 2005

Using the code

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.

Future work

Anyone can contribute:

  • Bugs: the code may not be bug free, so any bug reports will get fixed.
  • Support credentials for accessing secure download sites.

History

10/25/2006 - Update

  • Support for downloading from a list of URLs in case one of them is down (Zac Ruiz)
  • Added support for proxies

7/31/2006 - Update

  • Now supports ftp:// and file:// URLs (only HTTP is resumable, and supporting these adds a hard requirement to .NET 2.0). File URLs must be of the following form:
    • file:///c:/myfolder/myfile.txt, or
    • file://///networkMachine/sharedFolder/file.txt

      (Yes, that's five slashes.)

  • It's now possible to download a 0 byte file (an empty placeholder file gets created on disk)
  • More accurate exceptions in some cases

6/10/2006 - Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Phil Crosby


Member
I'm a recent graduate of the University of Maryland. I have interned with the Visual Studio team at Microsoft and was an Extreme Blue Intern at IBM. I'm currently hacking on InstallPad.com, Mono applications for GNOME, and Ink for Word (Tablet PC annotation support for Microsft Word). Visit my blog or homepage.
Occupation: Web Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
GeneralThank you very much for this API Pinmemberradialronnie5:48 23 Jul '08  
GeneralPhil. Thanks !!! a qeustion PinmemberMember 151816712:31 19 Feb '08  
QuestionGreat and easy solution Pinmembermishenkovks3:04 21 Sep '07  
GeneralMono Sharing Violation Pinmemberstclarence20:56 20 Jun '07  
QuestionHow can I use it in VB ? Pinmemberpongup11:41 10 May '07  
AnswerRe: How can I use it in VB ? Pinmemberrobertw01922:01 21 Jul '07  
GeneralDownload from Tomcat Webdav Pinmemberroychoo3:22 16 Mar '07  
General407 authentication error PinmemberCohen Shwartz Oren8:19 7 Jan '07  
GeneralRe: 407 authentication error Pinmemberveritas guy10:28 16 Jul '07  
NewsUI HAS BEEN POSTED FINALLY PinmemberFred Johanns11:38 18 Aug '06  
GeneralFuture improvement PinmemberTehPenguin16:20 9 Aug '06  
GeneralRe: Future improvement PinmemberPhil Crosby10:22 25 Oct '06  
GeneralReally nice Pinmemberajai80852:30 1 Aug '06  
GeneralNeat and simple! PinmemberSuper Lloyd6:00 29 Jul '06  
GeneralUI & a small bug Pinmemberderfsplat10:24 16 Jun '06  
GeneralRe: UI & a small bug PinmemberPhil Crosby11:21 16 Jun '06  
GeneralRe: UI & a small bug [modified] PinmembermyTarnold5:35 25 Jul '06  
GeneralRe: UI & a small bug PinmemberShakerWD11:23 26 Jul '06  
Generalremote server returned an error Pinmemberhelpsoft.ru2:36 15 Jun '06  
GeneralRe: remote server returned an error PinmemberPhil Crosby3:29 15 Jun '06  
GeneralRe: remote server returned an error Pinmemberhelpsoft.ru4:41 15 Jun '06  
Questiongreat! PinmemberHerre Kuijpers0:44 12 Jun '06  
AnswerRe: great! PinmemberPhil Crosby6:15 12 Jun '06  
AnswerRe: great! PinmemberPhil Crosby22:17 31 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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