簡體   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