C#Downloade下载进度/完成 [英] C# Downloade Download Progress/Complete

查看:129
本文介绍了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!

推荐答案

将ProgressChanged()方法内容更改为:



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


我没有遇到过这样的问题,但是我使用了不同的API,它允许更多的控制,更通用, System.Net.HttpWebRequest

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx [ ^ ]。



如需完整的工作样本,全部在一个文件中,请看我过去的答案:如何下载来自互联网的文件 [ ^ ]。



我的实用程序HttpDownloader允许继续下载部分下载的资源。这是可能的,特别是因为HTTP响应在一开始就为您提供了预期的总大小。这样,当下载数据的大小等于预期大小时,您可以检测进度并指示100%进度。



只是您可以考虑的替代方案,非常简单但更通用。



-SA
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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆