网络连接监视器VB.NET

时间:2014-03-20 06:32:34

标签: vb.net networking interface connection

我需要制作一个显示以下信息的软件: 连接状态 IP地址 连接正常运行时间 数据已发送 收到的数据 上传速度 下载速度 我试图以一种将所有网络接口的名称加载到组合框中的方式来实现。更改组合框选择后,String NetInt将更改为选择,但会显示网络接口无法转换为字符串的错误。

我想要做的是仅列出可能的接口并显示上述信息。是否有捷径可寻?我已经重写了5次代码,但我仍然无法弄明白。

Imports System.Net
Imports System.Net.NetworkInformation


Public Class Form1
Dim CurrentNetworkInterface As NetworkInterface
Delegate Sub FuncCallback(ByRef obj As Object, ByVal text As String)
Dim NetInts As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()

Private Function inArrayNetType(ByVal arr As NetworkInterfaceType(), ByVal match As NetworkInterfaceType) As Boolean
    For Each nettype As NetworkInterfaceType In arr
        If nettype = match Then
            Return True
        End If
    Next
    Return False
End Function

Public Function GetNetInteface() As List(Of NetworkInterface)
    Dim NetInts = NetworkInterface.GetAllNetworkInterfaces
    Dim NetList As New List(Of NetworkInterface)
    Dim NetInterfaceFilter As NetworkInterfaceType() = {NetworkInterfaceType.Ethernet, NetworkInterfaceType.Ppp, NetworkInterfaceType.Wireless80211}
    For Each NetInt As NetworkInterface In NetInts
        If inArrayNetType(NetInterfaceFilter, NetInt.NetworkInterfaceType) Then
            NetList.Add(NetInt)
        End If
    Next
    Return NetList
End Function

Private Sub LoadNicComboBox()
    NetInts = NetworkInterface.GetAllNetworkInterfaces
    Dim NetInterfaceFilter As NetworkInterfaceType() = {NetworkInterfaceType.Ethernet, NetworkInterfaceType.Ppp, NetworkInterfaceType.Wireless80211}
    If CBNetInts.InvokeRequired Then
        Dim d As New FuncCallback(AddressOf LoadNicComboBox)
        Me.Invoke(d, New Object() {Nothing, Nothing})
    Else
        CBNetInts.DataSource = GetNetInteface()
        CBNetInts.DisplayMember = "Name"
        CBNetInts.ValueMember = "Id"
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    LoadNicComboBox()
    Dim HostName As String
    Dim IPAdds() As IPAddress
    HostName = System.Net.Dns.GetHostName()
    IPAdds = Dns.GetHostAddresses(HostName)
    For i = 0 To IPAdds.Count - 2
        IPAddIn.Text = "Internal IP Address: " & (IPAdds(i).ToString)
    Next
    Using wb As New WebClient
        Dim s As String = wb.DownloadString("http://www.whatsmyip.us/showipsimple.php")
        Dim l As String = s.Remove(0, 16)
        Dim j As String = l.Remove(l.Length - 3, 3)
        IPAddEx.Text = "External IP Address: " & j
    End Using
End Sub

Private Sub TmrStatus_Tick(sender As Object, e As EventArgs) Handles TmrStatus.Tick
    If My.Computer.Network.IsAvailable = True Then
        LblStatus.Text = "Status: Connected"
        Dim NetSpd As Decimal = CurrentNetworkInterface.Speed / 1024 / 1024
        Dim NetSpdR = Math.Truncate(NetSpd)
        SpdLbl.Text = "Speed: " & NetSpdR & " Mbits/s"
        DatSent.Text = "Data Sent: " & Math.Truncate(CurrentNetworkInterface.GetIPv4Statistics.BytesSent / 1024) & " KB"
        DatRcvd.Text = "Data Received: " & Math.Truncate(CurrentNetworkInterface.GetIPv4Statistics.BytesReceived / 1024) & " KB"
    Else
        LblStatus.Text = "Status: No Internet Access"
    End If
End Sub

Private Sub CBNetInts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBNetInts.SelectedIndexChanged
    CurrentNetworkInterface = CBNetInts.SelectedItem
    Dim IpInterface As IPInterfaceProperties = CurrentNetworkInterface.GetIPProperties
    Dim UnicastIpaddr As UnicastIPAddressInformationCollection = IpInterface.UnicastAddresses
End Sub

结束班

0 个答案:

没有答案
相关问题