Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I added a progressbar and i wanted the program to launh the downloaded file but when the download is complete nothing happens at all!

C#
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("http://www.spadille.net/aos075install.msi", @"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;
                        System.Diagnostics.Process.Start("\\aos075install.msi");
                    }

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



thanks for help!
Posted
Comments
Sergey Alexandrovich Kryukov 5-Feb-13 14:14pm    
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?
—SA
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

Change your ProgressChanged() method content to this:

C#
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());
 
Share this answer
 
I did not experience such problems, but I use different API which allows for more control and is more universal, System.Net.HttpWebRequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].

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.

—SA
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900