why this code does not show the speed

时间:2017-06-20 12:32:23

标签: c#

I create a program for show me speed of internet. For example I need to show 26.365 KB/S but it show me this 270000000.

Code :

static void Main(string[] args)
{
    int KB = 1024;
    int MB = KB * KB;
    int GB = MB * KB;
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface nic in nics)
    {
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
            && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
            && nic.OperationalStatus == OperationalStatus.Up
            && nic.Name.StartsWith("vEthernet") == false
            && nic.Description.Contains("Hyper-v") == false)
        {
            Console.WriteLine(nic.Description);

            long speed = nic.Speed / KB;
            Console.WriteLine("=====================");
            if (speed < KB)
            {
                Console.WriteLine("{0} Byte", speed);
            }
            else
            {
                if (Convert.ToBoolean(speed < 100))
                {
                    Console.WriteLine("{0} KB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 1000))
                {
                    Console.WriteLine("{0} MB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 10000))
                {
                    Console.WriteLine("{0} GB/S", speed);
                }
                else if (Convert.ToBoolean(speed < 100000))
                {
                    Console.WriteLine("{0} TB/S", speed);
                }
            }
        }
    }
    Console.ReadKey();
}

Its not work. It not show me speed. Whats the problem? How can I solve this problem?

/************************************************************************/

3 个答案:

答案 0 :(得分:3)

在我看来,你错过了一些东西:

  1. 根据the NetworkInterface.Speed Property documentationSpeed属性每秒返回bits,因此在开始比较之前,我们应该将其转换为bytes除以8(不是KB)。
  2. 在进行比较时,我们应该将速度值与KBMBGBTB的占位符进行比较,而不是文字数字。这样可以减少错误并使其更易于阅读。例如,您将速度与10的倍数进行比较而不是1024.
  3. 您的if语句无法捕获所有条件,只有少于100000的条件。
  4. 我们需要将速度除以我们正在显示的尺寸(您只在开始时将其划分为KB
  5. 如果要显示小数位,我们应该在进行除法之前将数字转换为小数,然后将结果格式化为显示3位小数。
  6. 这是解决这些问题的一种方法:

    static void Main(string[] args)
    {
        int KB = 1024;
        int MB = KB * KB;
        int GB = MB * KB;
        long TB = (long)GB * KB;
    
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    
        foreach (NetworkInterface nic in nics)
        {
            if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback
                && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
                && nic.OperationalStatus == OperationalStatus.Up
                && nic.Name.StartsWith("vEthernet") == false
                && nic.Description.Contains("Hyper-v") == false)
            {
                Console.WriteLine(nic.Description);
    
                // 1. Convert bits to bytes
                // 5. Convert result to decimal
                decimal speed = (decimal)nic.Speed / 8;
    
                // 2. Do comparisons with our variables and
                // 3. Include all possible conditions (starting with > TB)
                if (speed >= TB)
                {
                    // 4. Divide the speed by the display size
                    // 5. Format the output to show 3 decimal places
                    Console.WriteLine("Speed: {0:0.000} TB/S", speed / TB);
                }
                else if (speed >= GB)
                {
                    Console.WriteLine("Speed: {0:0.000} GB/S", speed / GB);
                }
                else if (speed >= MB)
                {
                    Console.WriteLine("Speed: {0:0.000} MB/S", speed / MB);
                }
                else if (speed >= KB)
                {
                    Console.WriteLine("Speed: {0:0.000} KB/S", speed / KB);
                }
                else
                {
                    Console.WriteLine("Speed: {0:0.000} Bytes per second", speed);
                }
            }
        }
    
        Console.Write("\nDone!\nPress any key to exit...");
        Console.ReadKey();
    }
    

答案 1 :(得分:0)

问题出在你的条件中。

如果你的速度超过100000,那么你的代码就没有其他情况了。

您应该这样写(在最后if中移除else if):

if (speed < KB)
{
    Console.WriteLine("{0} Byte", speed);
}
else
{
    if (speed < 100)
    {
        Console.WriteLine("{0} KB/S", speed);
    }
    else if (speed < 1000)
    {
        Console.WriteLine("{0} MB/S", speed);
    }
    else if (speed < 10000)
    {
        Console.WriteLine("{0} GB/S", speed);
    }
    else 
    {
        Console.WriteLine("{0} TB/S", speed);
    }
}

此外,您无需像上面的代码那样转换结果。

答案 2 :(得分:-4)

当前实施

long speed = nic.Speed / KB;

相反,您应该使用支持十进制的数据类型

decimal speed = (decimal)nic.Speed/KB;