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?
/************************************************************************/
答案 0 :(得分:3)
在我看来,你错过了一些东西:
Speed
属性每秒返回bits
,因此在开始比较之前,我们应该将其转换为bytes
除以8(不是KB
)。 KB
,MB
,GB
和TB
的占位符进行比较,而不是文字数字。这样可以减少错误并使其更易于阅读。例如,您将速度与10的倍数进行比较而不是1024. if
语句无法捕获所有条件,只有少于100000的条件。KB
)这是解决这些问题的一种方法:
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;