/// <summary> /// 获取所有网卡综合的网速信息 /// </summary> /// <param name="nics"></param> /// <param name="oldNetworkSpeedInfo"></param> /// <param name="interval"></param> /// <returns></returns> public static NetworkSpeedInfo GetTotalNetworkSpeedInfo(NetworkInterface[] nics, NetworkSpeedInfo oldNetworkSpeedInfo = null, double interval = 1000) { NetworkSpeedInfo info = new NetworkSpeedInfo(); info.NIC = null; foreach (NetworkInterface nic in nics) { IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); info.BytesSent += interfaceStats.BytesSent; info.BytesReceived += interfaceStats.BytesReceived; } info.BytesSentSpeed += oldNetworkSpeedInfo == null ? 0 : (long)((info.BytesSent - oldNetworkSpeedInfo.BytesSent) / (interval / 1000)); info.BytesReceivedSpeed += oldNetworkSpeedInfo == null ? 0 : (long)((info.BytesReceived - oldNetworkSpeedInfo.BytesReceived) / (interval / 1000)); if (info.BytesSentSpeed < 0) { info.BytesSentSpeed = 0; } if (info.BytesReceivedSpeed < 0) { info.BytesReceivedSpeed = 0; } return(info); }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { // get network speed if (NotityInfoEvent != null) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces() .Where(n => n.OperationalStatus == OperationalStatus.Up) .ToArray(); networkSpeedInfo = NetworkSpeedInfo.GetTotalNetworkSpeedInfo(nics, networkSpeedInfo, timer.Interval); Notify(networkSpeedInfo); } }
/// <summary> /// 获取指定网卡的网速信息 /// </summary> /// <param name="nic"></param> /// <param name="oldNetworkSpeedInfo"></param> /// <param name="interval"></param> /// <returns></returns> public static NetworkSpeedInfo GetNetworkSpeedInfo(NetworkInterface nic, NetworkSpeedInfo oldNetworkSpeedInfo = null, double interval = 1000) { NetworkSpeedInfo info = new NetworkSpeedInfo(); info.NIC = nic; IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); info.BytesSent = interfaceStats.BytesSent; info.BytesReceived = interfaceStats.BytesReceived; info.BytesSentSpeed = oldNetworkSpeedInfo == null ? 0 : (long)((info.BytesSent - oldNetworkSpeedInfo.BytesSent) / (interval / 1000)); info.BytesReceivedSpeed = oldNetworkSpeedInfo == null ? 0 : (long)((info.BytesReceived - oldNetworkSpeedInfo.BytesReceived) / (interval / 1000)); if (info.BytesSentSpeed < 0) { info.BytesSentSpeed = 0; } if (info.BytesReceivedSpeed < 0) { info.BytesReceivedSpeed = 0; } return(info); }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { // get network speed if (NotityInfoEvent != null) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces() .Where(n => n.OperationalStatus == OperationalStatus.Up && (n.GetIPStatistics().BytesReceived != 0 || n.GetIPStatistics().BytesSent != 0)) .ToArray(); foreach (NetworkInterface nic in nics) { NetworkSpeedInfo ni = null; if (networkSpeedInfos.ContainsKey(nic.Id)) { ni = networkSpeedInfos[nic.Id]; } ni = NetworkSpeedInfo.GetNetworkSpeedInfo(nic, ni, timer.Interval); networkSpeedInfos[nic.Id] = ni; } Notify(networkSpeedInfos.Values.OrderByDescending(n => n.BytesReceivedSpeed).ToArray()); } }
public void Notify(NetworkSpeedInfo info) { NotityInfoEvent?.Invoke(this, info); }