Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.

C# Download File with Progress Bar

The following is a snippet of code to download a file in a Windows Forms application using C# and the System.Net.WebClient object. This code also updates a progress bar as the file is downloaded.

First, create a new windows forms application with C# as the target language. (NOTE: This works in any .NET framework version). Add a ProgressBar to the form and a BackgroundWorker control. Also add a Button to the form. The form will look something like this:

windows form application



We need some kind of background thread to update the progress bar while the file is downloaded. If we tried to update the progress bar from the main thread, out app will just hang, allowing no user input, and the progress bar would not change until the file was completely downloaded. Instead, we need to let the main thread run separately from our lengthy download operation. Set the BackgroundWorker's properties like this:

backgroundworker properties

Go to the Events view of the BackgroundWorker and double click each of its events so they are auto-wired up as shown here:

backgroundworker events

Double click the button to auto-wire up the event click event and call our background worker to start running asynchronously when the button is clicked. Your code will look something like this:

private void btnTestDownload_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

In the BackgroundWorker's DoWork method, we put the code to run the download on a separate thread. The code will look something like this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // the URL to download the file from
    string sUrlToReadFileFrom = "http://devtoolshed.googlepages.com/filezilla_download.exe";
    // the path to write the file to
    string sFilePathToWriteFileTo = "C:\\filezilla_download.exe";
 
    // first, we need to get the exact size (in bytes) of the file we are downloading
    Uri url = new Uri(sUrlToReadFileFrom);
    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    response.Close();
    // gets the size of the file in bytes
    Int64 iSize = response.ContentLength;
 
    // keeps track of the total bytes downloaded so we can update the progress bar
    Int64 iRunningByteTotal = 0;
 
    // use the webclient object to download the file
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        // open the file at the remote URL for reading
        using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
        {
            // using the FileStream object, we can write the downloaded bytes to the file system
            using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                // loop the stream and get the file into the byte buffer
                int iByteSize = 0;
                byte[] byteBuffer = new byte[iSize];
                while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    // write the bytes to the file system at the file path specified
                    streamLocal.Write(byteBuffer, 0, iByteSize);
                    iRunningByteTotal += iByteSize;
 
                    // calculate the progress out of a base "100"
                    double dIndex = (double)(iRunningByteTotal);
                    double dTotal = (double)byteBuffer.Length;
                    double dProgressPercentage = (dIndex / dTotal);
                    int iProgressPercentage = (int)(dProgressPercentage * 100);
 
                    // update the progress bar
                    backgroundWorker1.ReportProgress(iProgressPercentage);
                }
 
                // clean up the file stream
                streamLocal.Close();
            }
 
            // close the connection to the remote server
            streamRemote.Close();
        }
    }
}

Now implement the BackgroundWorker's ProgressChanged event to update the progress bar. NOTE: You can ONLY update the progress bar in this method. This method synchronized back with the form's main thread. If you tried to update the progress bar from the DoWork method, you would get an exception because that is not on the same thread as the form. Here's the code:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

For convenience, in the result you can show a message that your file is downloaded to note that it is completely finished like this:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    MessageBox.Show("File download complete");
}

Roberto

Thanks a lot!

Thank you

Thank you very much for this contribution! IT helped me pimp my progress bar while downloading an update for a application i've made!

Thnx again!

So much thanks

I have a trouble stream with progress bar in WPF. Then this sample code
give me a brilliant hint and help me solve the problem.
I am again really appreciate from you.

I get this error The remote

I get this error
The remote server returned an error: (403) Forbidden.

Another approach

Hello!

Here is an article - Show progress on long-running operations - is an article describing another approach to keep the UI responsive, starting long-running operations (like downloading) in separate thread. The approach from above mentioned article is logically similar to the current way, but without BackgroundWorker.

Very useful, but...

Hi,
thanks for this useful article!
I tried it out and encountered that you are allocating a buffersize equally to the contentlength. This means downloading huge files will eventually overload the ram. To limit the buffersize to 1024 byte, I just changed two lines:
byte[] byteBuffer = new byte[1024];
double dTotal = (double)iSize;
Now the process is running at about 5Mb of ram. instead of e.g. 500Mb when downloading such huge a file.

Regards
shyd

It is perfect

It is perfect!
Thank you very much!

Memory use

Looking at the code here this would be completely unsuitable for anything but small files since it creates a byte array the size of the file which leads to pretty bad memory use.

Good example code but for real use anyone looking at this code should implement a fixed size buffer and flush the buffer to file and reuse it to grab the next chunk of data.

In fact it would be simpler

In fact it would be simpler to use the DownloadFileAsync method in the WebClient class

http://www.csharp-examples.net/download-files/

This way has some negative effects

The user's pc can cache the file, and if you replace it with a new one on the server with the same name it will not replace it.

Very helpful

Thank u very much

Many Thanks

This codesnipet works like a charm and saved me lots of pain.

Thank you very much.

Kudos

I have been looking for a model to learn how to do this for a month. Even after posting questions to The Code Project, I haven't come accross anything that makes as muchs sense as this article.

Great job and thank you very much.

JijKgYDvBZXRZ

This is just the peerfct answer for all of us

Download progressBar1.zip

Project Run Success But problem is that when i Past or Write path in Source file at that time one error Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'.

TXLDnoMyeLVmZKXPTJT

Put in browser whetshouie.gov/blog/2010/10/19/adrienne-explains-how-college-students-are-benefiting-affordable-care-act. Lucky Adrienne. She volunteers in her community church because her parents continue to support her after they paid all her college expenses. Poor student paying off students loans (expenses another sham) has to work part-time because full-time work cannot be found in current economy and pays practically entire paycheck on coverage. This plan is a hand out to the rich.

excellent

this code work perfect....it's very use full for my project...

thnx a lot for this example

thnx a lot for this example :)) it's very usefull

the code works, but half the

the code works, but half the time it does one of the 2 things:

1) throws "Unable to read data from the transport connection: The connection was closed." at the line "iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length);"

2) background worker seems to freeze, and eventually throw the same error.

If i go one line at a time via debug, it works like a charm... it works if display a messagebox... it waits for a while intull the user cliks ok, and the code works, but if it runs at full speed... it rarely finishes.

what should i do ?

one solution would be to

one solution would be to close the stream then reopen and continue from where it left off... can anyone please show me how this would be done ?

A little problem

Using this code I have a little problem. I've followed it to the latest bit, and still it keeps jumping from 0% to 99%, and between them there is no notification, nor changes, nothing. Why is this happening?

Also forgot that cancellation

Also forgot that cancellation won't stop the process :S

cancellation

this is a little late, but here's my two cents!

you need to set the 'WorkerSupportsCancellation' property of the background worker to true, then implement some sort of button that will call the 'CancelAsync' method of the background worker, this sets the 'CancellationPending' property of the background worker to true. you then need to implement some sort of check to catch the change (if statements work well) and cancel the background worker there using the event arguments (in most cases, e.cancel works. remember to close any streams you have open! i think, but i'm not 100% sure, that if you implement the check inside a while loop you also need a break statement)

hope that helps! might be an easier way of doing it, but this works well for me. only been using c# for a few months, be happy to hear any better ways if there are any!

cracking article btw, i adapted this code for an ftp uploader and it worked a treat!

go

go

progress bar & XmlReader

Could I use this or any similiar technique with XmlReader?

Strange problem concerning the progress bar update

I created a new project (VS 2008, .Net 3.5) like shown here. Everything is working fine except the MessageBox "File download complete" is shown when the progess bar is at approximately 30 %. This seems to be a timing issue because when I debug the process and have a breakpoint in "backgroundWorker1_ProgressChanged", the progress bar is updated fine. A "progressBar1.Update();" doesn´t make any difference. Any help is appreciated.

Possible fix

I think I found the problem here, it occurs when the progress percentage is cast from a double to an int. Replace:
int iProgressPercentage = (int)dProgressPercentage * 100);
with
int iProgressPercentage = (int)(Math.Round(dProgressPercentage) * 100);
And it seems to work much better! Hope this helps

Oops

Spoke too soon, just checked it now and it's rubbish, ignore that haha

The only way this could happen

The only way this could be happening is if your download is failing early and exiting with a success case.  This could be due to network traffic or some other issue.  Make sure you did not accidently put a message box into your worker thread.  Then make sure there are no early outs in your code that could cause it to finish at 30%. 

Worked smoothly

Thanks for posting such a wonderful information. It was a perfect solution for my problem.

Thanks

Nice helpful, working post. Thanks.

Change the buffer type to long instead of int

90% sure that the issue with the arithmetic operation is because the file size is bigger than the Int32 size. Just change the buffer to a long (Int64) and try it again.

arithmetic operation resulted in an overflow

This works fine with small file but trying to download a bigger file (75MBs) i get an "arithmetic operation resulted in an overflow" exception..

any idea how to solve this?

thanks

Hiiii

Please give sample source code

What an article

Marvellous.......................

Streaming a large file from the hard drive

This code above is interested in only downloading a file from the web.  To read in a large file from your hard drive such as a very large XML file, probably the best way to do this is using the FileStream object.  You can find out more about it here:

http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx

This will allow you to stream the file in one character at a time.  Inside your loop that reads the file, you can update your progress bar and update your TreeView control.

NOTE: Make sure you are doing all of this streaming on a separate thread such as a background worker process.  That way, your UI will update.  If you run this streaming on your main thread, you will not see the progress bar or any other UI update and it will appear to "hang" until your file is completely streamed in.

hi thx for the code it work

hi thx for the code it work ok
but how to catch download errors when the file does not exists or no internet connection avaiable?

Catching download errors

There are a couple of ways to do this. If you attempt to download a file that does not exist (invalid URL) or that may have a connection drop/timeout, then there can be an exception thrown here.

If you want the code to prompt the user and let them retry, then put a try/catch block around the code in the background worker DoWork method. When the exception is thrown, allow them to restart the download with a dialog or other message box.

If you want to abort the operation, then you don't have to do anything because the background worker already handles exception packaging across threads for you. Just let the exception get thrown. Then in the background worker RunWorkerCompleted() method, you can get the result by checking the "e" RunWorkerCompletedEventArgs parameter. Check if the Error property is = null or not. If it is not null, then there will be the exception which you can display to the user there.

To download file from hard drive using progress bar & Background

Hi,
I have created a project to load xml file in Treeview in winform.
Now i want to add a progress bar which will show the time taken to get file from my memory to treeview.i want to use a background worker also.
I have treeview,Browse button(to select a file), textbox(to display the file name selected)
toolStripStatusLabel,toolStripStatusProgressBar.
Can you please help out..

This is very helpful article

Mine progressbar moves upto 6 count and then nothing else works. I have tried timer method, and other progress bar invalidate method. But from al these i have similar method. I cannot put background thread. Its very complex code and i cant change in between. Is there any other possible solution to update progressbar using the same main thread?

Update ProgressBar from main thread

Updating controls from your same thread during blocking operations like downloading a file will never work as well as a separate thread. You should really get into the habit of doing the updating from separate threads because this is core to doing advanced windows programming.

All of that being said, I did find a thread where you can create a delegate back to your thread that may work for you. Here is the post:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/093dfdb2-e...

Cross-thread operation not valid

Cross-thread operation not valid

async????

async