本文介绍了C#Downloade下载进度/完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个进度条,我希望该程序能够启动下载的文件但是下载完成时根本没有任何事情发生!



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("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!

推荐答案

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());



这篇关于C#Downloade下载进度/完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:28