简体   繁体   English

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

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

I'm new to programming in C# and im trying to create an Async downloader because WebClient is just to slow for a 500mb+ zip file.我是在 C# 中编程的新手,我正在尝试创建一个异步下载器,因为 WebClient 对于 500mb+ zip 文件来说速度太慢了。 Below is the code I'm working with and these are the lines im having issues with updating my progressbar and label下面是我正在使用的代码,这些是我在更新我的进度条和 label 时遇到问题的行

PROBLEM CODE:问题代码:

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

CODE代码

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

I ended up using this package -> https://github.com/bezzad/Downloader我最终使用了这个 package -> https://github.com/bezzad/Downloader

暂无
暂无

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

相关问题 C#System.FormatException:输入字符串的格式不正确 - C# System.FormatException: Input string was not in a correct format System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException:输入字符串的格式不正确 - System.FormatException:Input string was not in a correct format System.FormatException:输入字符串的格式不正确 - System.FormatException: Input string was not in a correct format C# 错误 System.FormatException: '输入字符串的格式不正确。' 在数字输入上 - C# Error System.FormatException: 'Input string was not in a correct format.' on number input System.FormatException: '输入字符串的格式不正确。' 在 C# 黄皮书教科书中 - System.FormatException: 'Input string was not in a correct format.' in C# Yellowbook textbook c# 如何修复:“未处理的异常:System.FormatException:输入字符串的格式不正确。” - c# How Can I Fix: “Unhandled Exception: System.FormatException: Input string was not in a correct format.” C# Excel combobox 中的空行(导致错误“System.FormatException:输入字符串格式不正确。”) - C# Excel empty rows in combobox (causing error “System.FormatException:Input string was not in a correct format.”) 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format 错误:System.FormatException:输入字符串的格式不正确 - ERROR : System.FormatException: Input string was not in the correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM