繁体   English   中英

C# 下载方法 - System.FormatException: '输入字符串的格式不正确'

[英]C# Download Method - System.FormatException: 'Input string was not in a correct format'

我是在 C# 中编程的新手,我正在尝试创建一个异步下载器,因为 WebClient 对于 500mb+ zip 文件来说速度太慢了。 下面是我正在使用的代码,这些是我在更新我的进度条和 label 时遇到问题的行

问题代码:

if (progressBar.InvokeRequired)
                            {
                                progressBar.Invoke(new Action(() => progressBar.Show()));
                                progressBar.Invoke(new Action(() => progressBar.Value = int.Parse(Math.Truncate(percentage).ToString())));
                            }
                            else
                            {
                                progressBar.Invoke(new Action(() => progressBar.Show()));
                                progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
                            }

                            if (labelProgress.InvokeRequired)
                            {
                                labelProgress.Invoke(new Action(() => labelProgress.Show()));
                                labelProgress.Invoke(new Action(() => labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString()));
                            }
                            else
                            {
                                labelProgress.Invoke(new Action(() => labelProgress.Show()));
                                labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString();
                            }

代码

private void Downloader_Click(object sender, EventArgs e)
        {
            Downloader.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Downloader.Enabled = true;
            System.Windows.MessageBox.Show("Download Completed");
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DownloadFileWithProgress(zipUrl, zipPath, progressBar, labelPerc);
        }
        private void DownloadFileWithProgress(string DownloadLink, string TargetPath, Bunifu.UI.Winforms.BunifuProgressBar progressBar, Label labelProgress)
        {
            int bytesProcessed = 0;
            Stream remoteStream = null;
            Stream localStream = null;
            WebResponse response = null;

            try
            {
                WebRequest request = WebRequest.Create(DownloadLink);
                if (request != null)
                {
                    double TotalBytesToReceive = 0;
                    var SizewebRequest = HttpWebRequest.Create(new Uri(DownloadLink));
                    SizewebRequest.Method = "HEAD";

                    using (var webResponse = SizewebRequest.GetResponse())
                    {
                        var fileSize = webResponse.Headers.Get("Content-Lenght");
                        TotalBytesToReceive = Convert.ToDouble(fileSize);
                    }

                    response = request.GetResponse();
                    if (response != null)
                    {
                        remoteStream = response.GetResponseStream();
                        string filePath = TargetPath;
                        localStream = File.Create(filePath);
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;

                        do
                        {
                            bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
                            localStream.Write(buffer, 0, bytesRead);
                            bytesProcessed += bytesRead;
                            double bytesIn = double.Parse(bytesProcessed.ToString());
                            double percentage = bytesIn / TotalBytesToReceive * 100;
                            percentage = Math.Round(percentage, 0);

                            if (progressBar.InvokeRequired)
                            {
                                progressBar.Invoke(new Action(() => progressBar.Show()));
                                progressBar.Invoke(new Action(() => progressBar.Value = int.Parse(Math.Truncate(percentage).ToString())));
                            }
                            else
                            {
                                progressBar.Invoke(new Action(() => progressBar.Show()));
                                progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
                            }

                            if (labelProgress.InvokeRequired)
                            {
                                labelProgress.Invoke(new Action(() => labelProgress.Show()));
                                labelProgress.Invoke(new Action(() => labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString()));
                            }
                            else
                            {
                                labelProgress.Invoke(new Action(() => labelProgress.Show()));
                                labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString();
                            }
                        } while (bytesRead > 0);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (response != null) response.Close();
                if (remoteStream != null) remoteStream.Close();
                if (localStream != null) localStream.Close();
            }
        }

我最终使用了这个 package -> https://github.com/bezzad/Downloader

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM