为什么我的wpf应用程序在捕获网卡速度时获得值-2097152?
我开发了一个应用程序,可以捕获网卡速度,我使用以下代码获取数据:为什么我的wpf应用程序在捕获网卡速度时获得值-2097152?,wpf,string,int64,int32,Wpf,String,Int64,Int32,我开发了一个应用程序,可以捕获网卡速度,我使用以下代码获取数据: NetworkInterface nic = nicArr[0]; IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); Int64 bytesSentSpeed = (Int64)(interfaceStats.BytesSent - uploadSpeed) / 1024; uploadSpeed = double.Parse(interfaceSt
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?似乎你有
上传速度
和下载速度
作为浮动
,对于字节接收速度
你将表达式转换为int
,因此如果你在(interfaceStats.BytesReceived-downloadSpeed)中有值
就大到2147483647(int.MaxValue
),则在向下转换(int)
将所有操作都设置为浮点值,只设置字典的格式值。看起来像是溢出异常。但您没有显示全部情况:
uploadSpeed
和downloadSpeed
的定义丢失,这是在while循环内吗?它不是在循环内。这是溢出异常问题,请将int改为Int64,我解决了这个问题,谢谢!谢谢你的帮助!我将int改为Int64,然后我解决了这个问题。