Click here to Skip to main content
12,747,719 members (38,279 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
I added a progressbar and i wanted the program to launh the downloaded file but when the download is complete nothing happens at all!

public void button4_Click(object sender, EventArgs e)
            using (System.IO.FileStream fs = System.IO.File.Create("\\aos075install.msi")) ;

            progressBar.Visible = true;

            WebClient webClient = new WebClient();

            webClient.DownloadFile("", @"C:\aos075install.msi");

            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

             void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
                        progressBar.Value = e.ProgressPercentage;

              void Completed(object sender, AsyncCompletedEventArgs e)
                        MessageBox.Show("Download Successfull!");
                        progressBar.Visible = false;

              private void button5_Click(object sender, EventArgs e)
                        System.Diagnostics.Process.Start("\\Ace of Spades\\readme.txt");

thanks for help!
Posted 5-Feb-13 8:50am
What, you don't see 100% in ProgressChanges? How do you know that it's complete?
Did you run it under debugger to see if Completed is called, ever? Does it happen to all URLs you try, or this is only one case?
jibesh 5-Feb-13 14:17pm
"when the download is complete nothing happens at all" is really nothing to us. you should tell what is the real problem you are facing. Please keep in mind that we cannot see your monitor,HDD or complete source code so you must provide enough details to solve your problem.

If you are expecting to start the MSI installed once the download is complete, check the path where you copied the file and running using Process start, they are different
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Change your ProgressChanged() method content to this:

double bytesIn = double.Parse(e.BytesReceived.ToString());
     double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
     double percentage = bytesIn / totalBytes * 100;
     progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I did not experience such problems, but I use different API which allows for more control and is more universal, System.Net.HttpWebRequest:[^].

For a complete working sample, all in a single file, please see my past answer: how to download a file from internet[^].

My utility, HttpDownloader, allows to continue downloading of partially downloaded resource. This is possible, in particular, because HTTP response gives you the expected total size in the very beginning. This way, you can detect progress and indicate 100% progress when the size of the downloaded data becomes equal to the expected size.

Just the alternative you could consider, a very simple one but more universal.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100