计算下载/上传速度错误

本文关键字:速度 错误 下载 计算 | 更新日期: 2023-09-27 18:21:25

我的下载/上传速度计算器有问题。我把数字写在"inputBox"中,我认为它无法解析它。这是源代码:

更新!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;
namespace Tomco_DownloadTime {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            timeLabel.Text = DateTime.Now.ToString("HH:mm tt");
        }
        IPv4InterfaceStatistics stat = NetworkInterface.GetAllNetworkInterfaces() [0].GetIPv4Statistics();
        private double getKBDownloadSpeed() {
            return (stat.BytesReceived)/1024;
        }
        private double getMBDownloadSpeed() {
            return ((stat.BytesReceived)/1024)/1024;
        }
        private double getGBDownloadSpeed() {
            return (((stat.BytesReceived)/1024)/1024)/1024;
        }
        private double getKBUploadSpeed() {
             return (stat.BytesSent)/1024;
        }
        private double getMBUploadSpeed() {
             return ((stat.BytesSent)/1024)/1024;
        }
        private double getGBUploadSpeed() {
            return (((stat.BytesSent)/1024)/1024)/1024;
        }
        private void startButton_Click(object sender,EventArgs e) {
            this.Width = 335;
            this.Height = 154;
            double size = double.Parse(inputBox.Text);
            if(roundingCheckBox.Checked == true) {
                if(downloadCheckBox.Checked == true) {
                    // download
                    if(kbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (KB):";
                        double kbSpeed = size / getKBDownloadSpeed();
                        outputLabel.Text = kbSpeed.ToString();
                    } 
                    if(mbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (MB):";
                    }
                    if(gbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (GB):";
                    }
                }
                if(uploadCheckBox.Checked) {
                        //upload
                    if(kbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (KB):";
                    }
                    if(mbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (MB):";
                    }
                    if(gbCheckBox.Checked) {
                        sizeTypeLabel.Text = "Size (GB):";
                    }
                }
            } else {
            }
        }
        private void optionsButton_Click(object sender,EventArgs e) {
            this.Width = 335;
            this.Height = 241;
        }
     }
  }

为什么我认为解析有问题?因为如果我将数字添加到inputBox,然后按下"计算"按钮,我的MessageBox就会出现,并显示错误。

计算下载/上传速度错误

这似乎是由无法识别的十进制分隔符引起的。我想这个代码不是在美国文化下运行的。

你可以试试:

double size = 
  double.Parse(inputBox.Text, System.Globalization.CultureInfo.InvariantCulture)