1. ''' <summary> 2. ''' ネットワーク使用率を求める。 3. ''' </summary> 4. Public Sub percentDemand() 5. ' 四捨五入する桁数 6. Const SHORT_NUMBER_ROUND As Short = 4 7. ' 占有率を求める際の百分率 Const SHORT_PERCENTAGE As Short = 100 8. ' ネットワーク使用率(受信) 9. Dim dReceivedPercent As Double = 0 ' ネットワーク使用率(送信) Dim dSentPercent As Double = 0 10. 11. ' 計算系の汎用クラス 12. Dim objMyTimeMath As New MyTimeMath() 13. Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces() 14. 15. For Each adapter As NetworkInterface In nics 16. If adapter.OperationalStatus.Equals(OperationalStatus.Up) AndAlso _ Not adapter.NetworkInterfaceType.Equals(NetworkInterfaceType.Loopback) AndAlso _ Not adapter.NetworkInterfaceType.Equals(NetworkInterfaceType.Tunnel) Then 17. If adapter.Supports(NetworkInterfaceComponent.IPv4) Then 18. Dim ipv4 As IPv4InterfaceStatistics = adapter.GetIPv4Statistics() 19. 20. If Not lReciveTmp.Equals(0) Then 21. 22. ' 受信パケット数 23. Dim lReceived As Long = ipv4.BytesReceived ' 送信パケット数 Dim lSent As Long = ipv4.BytesSent ' スピード 26. Dim iSpeed As Double = CDbl(adapter.Speed) / 10 27. dReceivedPercent = (lReceived - Me.lReciveTmp) * CDbl(SHORT_PERCENTAGE) / (iSpeed) 28. dSentPercent = (lSent - Me.lSentTmp) * CDbl(SHORT_PERCENTAGE) / (iSpeed) 29. Me.lReciveTmp = lReceived 30. Me.lSentTmp = lSent 31. Else 32. Me.lReciveTmp = ipv4.BytesReceived 33. End If 34. 35. dReceivedPercent = objMyTimeMath.decimalPointOmit(dReceivedPercent, SHORT_NUMBER_ROUND) 36. dSentPercent = objMyTimeMath.decimalPointOmit(dSentPercent, SHORT_NUMBER_ROUND) 37. 38. Me.dReceivedPercent = dReceivedPercent 39. Me.dSentPercent = dSentPercent 40. Me.dSentReceivedPercent = dReceivedPercent + dSentPercent 41. End If 42. End If 43. Next 44. 45. objMyTimeMath = Nothing 46. End Sub
1. ''' <summary> 2. ''' 小数点を省略する。 3. ''' </summary> 4. ''' <param name="dPercent">省略する値</param> 5. ''' <param name="dNumberRound">省略する桁数</param> 6. Public Function decimalPointOmit(ByVal dPercent As Double, ByVal sNumberRound As Short) As Double 7. ' 最低値の小数点 8. Const DOUBLE_DECIMAL_POINT As Double = 0.01 9. 10. If dPercent < DOUBLE_DECIMAL_POINT Then 11. dPercent = 0 12. ElseIf dPercent.ToString().StartsWith("0.") Then 13. If dPercent.ToString().Length > sNumberRound Then 14. dPercent = Double.Parse(dPercent.ToString().Substring(0, sNumberRound)) 15. End If 16. End If 17. 18. Return dPercent 19. End Function