为什么我的 wpf 应用程序在捕获网卡速度时得到值 -2097152?

why my wpf application get value -2097152 when capture Network card speeds?

我开发了一个捕获网卡速度的应用程序,我使用下面的代码来获取数据:

NetworkInterface nic = nicArr[0];
IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
Int64 bytesSentSpeed = (Int64)(interfaceStats.BytesSent - uploadSpeed) / 1024;
uploadSpeed = double.Parse(interfaceStats.BytesSent.ToString());
Int64 bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - downloadSpeed) / 1024;
downloadSpeed = double.Parse(interfaceStats.BytesReceived.ToString());
UpDownSpeed.Add("UpSpeed", bytesSentSpeed.ToString());
UpDownSpeed.Add("DownSpeed", bytesReceivedSpeed.ToString());

然后我得到值:

UpDpwnSpeed["UpSpeed"].ToString()

为什么我得到一个数字 -2097152?

您似乎有 uploadSpeeddownloadSpeed 作为 float,对于 bytesReceivedSpeed 您将表达式转换为 int,所以如果您有(interfaceStats.BytesReceived - downloadSpeed) 中的值大于 2147483647(int.MaxValue),然后在向下转换时变成负值 (int).
将所有操作设为浮点数,并为您的字典设置格式值。