Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from web to local file with progress meter in C#

Tags:

c#

.net-3.0

I have a small app that downloads some files from a remote (HTTP) server to the users local hard drive, some of the files are large, but I don't know just how large at run time. Is there any method that will allow me download a file with some type of progress meter?

This is a WinForms app, right now I'm using WebClient.DownloadFile() to download the file.

Edit: I've looked into the DownloadProgressChanged and OnDownloadProgressChanged events and they seem to work fine, but they will not work for my solution. I am downloading several files and if I use WebClient.DownloadFileAsync then the event is called several times/second because each file calls it.
Here is the basic structure of the app:

  • Download a list of files typically about 114
  • Run a loop over the list of files and download each one to its desination

I don't mind downloading each file seperatly but without downloading them with DownloadFileAsync() I cannot use the event handlers.

like image 816
UnkwnTech Avatar asked Jan 19 '26 08:01

UnkwnTech


2 Answers

Use WebClient.OnDownloadProgressChanged. Keep in mind, it's only possible to calculate progress if the server reports the size up front.

EDIT:

Looking at your update, what you can try is making a queue of URLs. Then, when a file finishes downloading (DownloadDataCompleted event), you will launch the async download of the next URL in the queue. I haven't tested this.

like image 89
Matthew Flaschen Avatar answered Jan 21 '26 22:01

Matthew Flaschen


Handle the WebClient DownloadProgressChanged event.

like image 41
stevehipwell Avatar answered Jan 21 '26 21:01

stevehipwell