显示连接适配器

时间:2014-10-18 09:23:46

标签: vb.net ip

我想在一台计算机上只显示带有IP地址的连接适配器。目前我有这个代码,但它也显示环回适配器。如何才能仅显示操作状态?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Interfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces
    Dim adapter As NetworkInterface
    Dim myAdapterProps As IPInterfaceProperties = Nothing
    Dim myGateways As GatewayIPAddressInformationCollection = Nothing

    For Each adapter In Interfaces
        TextBox1.AppendText("" & Environment.NewLine)
        If adapter.NetworkInterfaceType = NetworkInterfaceType.Loopback Then
            Continue For
        End If
        TextBox1.AppendText(adapter.Name & Environment.NewLine)
        TextBox1.AppendText(adapter.Description & Environment.NewLine)
        myAdapterProps = adapter.GetIPProperties
        myGateways = myAdapterProps.GatewayAddresses
        Dim IPInfo As UnicastIPAddressInformationCollection = adapter.GetIPProperties().UnicastAddresses
        Dim properties As IPInterfaceProperties = adapter.GetIPProperties()

        For Each IPAddressInfo As UnicastIPAddressInformation In IPInfo

            TextBox1.AppendText("IP Address : " & IPAddressInfo.Address.ToString & Environment.NewLine)

    Next
End Sub

1 个答案:

答案 0 :(得分:0)

你快到了。如果您只想查看状态为向上的适配器,请在continue分支中排除所有不具有此状态的适配器:

adapter.OperationalStatus <> OperationalStatus.Up

如果您还想从输出中删除Teredo适配器,请在名称包含单词 Teredo 时排除适配器:

adapter.Name.Contains("Teredo")

修改后的代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Interfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces
Dim adapter As NetworkInterface
Dim myAdapterProps As IPInterfaceProperties = Nothing
Dim myGateways As GatewayIPAddressInformationCollection = Nothing

For Each adapter In Interfaces
    TextBox1.AppendText("" & Environment.NewLine)
    If adapter.NetworkInterfaceType = NetworkInterfaceType.Loopback _
    or adapter.OperationalStatus <> OperationalStatus.Up _
    or adapter.Name.Contains("Teredo") Then
        Continue For
    End If
    TextBox1.AppendText(adapter.Name & Environment.NewLine)
    TextBox1.AppendText(adapter.Description & Environment.NewLine)
    myAdapterProps = adapter.GetIPProperties
    myGateways = myAdapterProps.GatewayAddresses
    Dim IPInfo As UnicastIPAddressInformationCollection = adapter.GetIPProperties().UnicastAddresses
    Dim properties As IPInterfaceProperties = adapter.GetIPProperties()

    For Each IPAddressInfo As UnicastIPAddressInformation In IPInfo
        TextBox1.AppendText("IP Address : " & IPAddressInfo.Address.ToString & Environment.NewLine)
    Next IPAddressInfo
Next

End Sub

这应该只显示非本地up-adapter和 Teredo 适配器。