版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
简要说明一下如何在代码中使用上述类的属性及方法
Dim netinterfaces() As NetworkInterface
netinterfaces = NetworkInterface.GetAllNetworkInterfaces
通过NetworkInterface.GetAllNetworkInterfaces的方法返回所有的网络接口,然后使用for each循环获得各个网络接口的信息。
IPAddressInformationCollection、IPAddressCollectio、GatewayIPAddressInformationCollection、MulticastIPAddressInformationCollection、UnicastIPAddressInformationCollection实际上都是包含对应IPAddressInformation、IPAddress、GatewayIPAddressInformation、MulticastIPAddressInformation、UnicastIPAddressInformation的集合。
使用for each循环就可以获得单个的信息,例如:
Dim AnycastAddrInfoCollection As IPAddressInformationCollection = IPIProperty.AnycastAddresses
For Each ipAddrInfo As IPAddressInformation In AnycastAddrInfoCollection
TextBox1.Text &= "广播IP地址:" & ipAddrInfo.Address.ToString & vbCrLf
Next
完整代码如下:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = "网络连接可用:" & NetworkInterface.GetIsNetworkAvailable & vbCrLf
TextBox1.Text &= "IPv4 环回接口的索引:" & NetworkInterface.LoopbackInterfaceIndex & vbCrLf
TextBox1.Text &= "IPv6 环回接口的索引:" & NetworkInterface.IPv6LoopbackInterfaceIndex & vbCrLf
TextBox1.Text &= "==========================================================================" & vbCrLf
Dim netinterfaces() As NetworkInterface
netinterfaces = NetworkInterface.GetAllNetworkInterfaces
For Each netinterface As NetworkInterface In netinterfaces
TextBox1.Text &= "网络接口标识符:" & netinterface.Id & vbCrLf
TextBox1.Text &= "网络接口名称:" & netinterface.Name & vbCrLf
TextBox1.Text &= "网络接口描述:" & netinterface.Description & vbCrLf
TextBox1.Text &= "网络接口的速度:" & netinterface.Speed & vbCrLf
Dim netType As String = ""
Select Case netinterface.NetworkInterfaceType
Case NetworkInterfaceType.Ethernet
netType = "Ethernet"
Case NetworkInterfaceType.Fddi
netType = "Fddi"
Case NetworkInterfaceType.Atm
netType = "Atm"
Case NetworkInterfaceType.Ppp
netType = "Ppp"
Case NetworkInterfaceType.TokenRing
netType = "TokenRing"
Case NetworkInterfaceType.Wireless80211
netType = "Wireless80211"
Case NetworkInterfaceType.Loopback
netType = "Loopback"
Case NetworkInterfaceType.Unknown
netType = "Unknown"
Case Else '还有很多种,请参看Msdn上的说明
netType = "else"
End Select
TextBox1.Text &= "网络接口类型:" & netType & vbCrLf
Dim netStatus As String = ""
Select Case netinterface.OperationalStatus
Case OperationalStatus.Dormant
netStatus = "不处于传输数据包的状态"
Case OperationalStatus.Down
netStatus = "无法传输数据包"
Case OperationalStatus.LowerLayerDown
netStatus = "无法传输数据包,因为它运行在一个或多个其他接口之上"
Case OperationalStatus.NotPresent
netStatus = "缺少组件(通常为硬件组件),无法传输数据包"
Case OperationalStatus.Testing
netStatus = "正在运行测试"
Case OperationalStatus.Unknown
netStatus = "状态未知"
Case OperationalStatus.Up
netStatus = "可以传输数据包"
Case Else
netStatus = "未知状态"
End Select
TextBox1.Text &= "网络连接的当前操作状态:" & netStatus & vbCrLf
TextBox1.Text &= "启用网络接口以接收多路广播数据包:" & netinterface.SupportsMulticast & vbCrLf
Dim mac As PhysicalAddress = netinterface.GetPhysicalAddress()
TextBox1.Text &= "MAC地址:" & mac.ToString() & vbCrLf
TextBox1.Text &= "是否支持IPv4:" & netinterface.Supports(NetworkInterfaceComponent.IPv4) & vbCrLf
TextBox1.Text &= "是否支持IPv6:" & netinterface.Supports(NetworkInterfaceComponent.IPv6) & vbCrLf
TextBox1.Text &= "== 网络接口信息 ==" & vbCrLf
Dim IPIProperty As IPInterfaceProperties = netinterface.GetIPProperties
Dim AnycastAddrInfoCollection As IPAddressInformationCollection = IPIProperty.AnycastAddresses
For Each ipAddrInfo As IPAddressInformation In AnycastAddrInfoCollection
TextBox1.Text &= "广播IP地址:" & ipAddrInfo.Address.ToString & vbCrLf
Next
Dim DhcpAddrCollection As IPAddressCollection = IPIProperty.DhcpServerAddresses
For Each ipAddrInfo As IPAddress In DhcpAddrCollection
TextBox1.Text &= "DHCP服务器的地址:" & ipAddrInfo.ToString & vbCrLf
Next
Dim DnsAddrCollection As IPAddressCollection = IPIProperty.DnsAddresses
For Each ipAddrInfo As IPAddress In DnsAddrCollection
TextBox1.Text &= "DNS服务器的地址:" & ipAddrInfo.ToString & vbCrLf
Next
Dim GatewayAddrCollection As GatewayIPAddressInformationCollection = IPIProperty.GatewayAddresses
For Each ipAddrInfo As GatewayIPAddressInformation In GatewayAddrCollection
TextBox1.Text &= "网关的地址:" & ipAddrInfo.Address.ToString & vbCrLf
Next
Dim MIPAddrCollection As MulticastIPAddressInformationCollection = IPIProperty.MulticastAddresses
For Each ipAddrInfo As MulticastIPAddressInformation In MIPAddrCollection
TextBox1.Text &= "多路广播地址:" & ipAddrInfo.Address.ToString & vbCrLf
Next
Dim UIPAddrCollection As UnicastIPAddressInformationCollection = IPIProperty.UnicastAddresses
For Each ipAddrInfo As UnicastIPAddressInformation In UIPAddrCollection
TextBox1.Text &= "单播地址:" & ipAddrInfo.Address.ToString & vbCrLf
TextBox1.Text &= "IPv4 掩码:" & ipAddrInfo.IPv4Mask.ToString & vbCrLf
Next
Dim WinsAddrCollection As IPAddressCollection = IPIProperty.WinsServersAddresses
For Each ipAddrInfo As IPAddress In WinsAddrCollection
TextBox1.Text &= "Wins服务器的地址:" & ipAddrInfo.ToString & vbCrLf
Next
TextBox1.Text &= "== IPv4信息 ==" & vbCrLf
Dim ip4 As IPv4InterfaceProperties = IPIProperty.GetIPv4Properties
If ip4 Is Nothing Then
TextBox1.Text &= "无" & vbCrLf
Else
TextBox1.Text &= "索引:" & ip4.Index & vbCrLf
TextBox1.Text &= "具有自动专用 IP 寻址:" & ip4.IsAutomaticPrivateAddressingActive & vbCrLf
TextBox1.Text &= "启用自动专用 IP 寻址:" & ip4.IsAutomaticPrivateAddressingEnabled & vbCrLf
TextBox1.Text &= "DHCP来获取 IP 地址:" & ip4.IsDhcpEnabled & vbCrLf
TextBox1.Text &= "转发(路由)数据包:" & ip4.IsForwardingEnabled & vbCrLf
TextBox1.Text &= "最大传输单位 :" & ip4.Mtu & vbCrLf
TextBox1.Text &= "使用WINS:" & ip4.UsesWins & vbCrLf
End If
TextBox1.Text &= "== IPv6信息 ==" & vbCrLf
Dim ip6 As IPv6InterfaceProperties = IPIProperty.GetIPv6Properties
If ip6 Is Nothing Then
TextBox1.Text &= "无" & vbCrLf
Else
TextBox1.Text &= "索引:" & ip6.Index & vbCrLf
TextBox1.Text &= "最大传输单位 :" & ip6.Mtu & vbCrLf
End If
TextBox1.Text &= "== IPv4统计数据 ==" & vbCrLf
Dim IPv4IS As IPv4InterfaceStatistics = netinterface.GetIPv4Statistics
TextBox1.Text &= "接收到的字节数:" & IPv4IS.BytesReceived & vbCrLf
TextBox1.Text &= "发送的字节数:" & IPv4IS.BytesSent & vbCrLf
TextBox1.Text &= "丢弃的传入数据包个数:" & IPv4IS.IncomingPacketsDiscarded & vbCrLf
TextBox1.Text &= "有错误的传入数据包数:" & IPv4IS.IncomingPacketsWithErrors & vbCrLf
TextBox1.Text &= "未知协议的传入数据包的数量:" & IPv4IS.IncomingUnknownProtocolPackets & vbCrLf
TextBox1.Text &= "接收的非单播数据包个数:" & IPv4IS.NonUnicastPacketsReceived & vbCrLf
TextBox1.Text &= "发送的非单播数据包个数:" & IPv4IS.NonUnicastPacketsSent & vbCrLf
TextBox1.Text &= "丢弃的传出数据包数:" & IPv4IS.OutgoingPacketsDiscarded & vbCrLf
TextBox1.Text &= "有错误的传出数据包数:" & IPv4IS.OutgoingPacketsWithErrors & vbCrLf
TextBox1.Text &= "输出队列的长度:" & IPv4IS.OutputQueueLength & vbCrLf
TextBox1.Text &= "接收到的单播数据包个数:" & IPv4IS.UnicastPacketsReceived & vbCrLf
TextBox1.Text &= "发送的单播数据包个数:" & IPv4IS.UnicastPacketsSent & vbCrLf
TextBox1.Text &= "== IP统计数据 ==" & vbCrLf
Dim IPIS As IPInterfaceStatistics = netinterface.GetIPStatistics
TextBox1.Text &= "接收到的字节数:" & IPIS.BytesReceived & vbCrLf
TextBox1.Text &= "发送的字节数:" & IPIS.BytesSent & vbCrLf
TextBox1.Text &= "丢弃的传入数据包个数:" & IPIS.IncomingPacketsDiscarded & vbCrLf
TextBox1.Text &= "有错误的传入数据包数:" & IPIS.IncomingPacketsWithErrors & vbCrLf
TextBox1.Text &= "未知协议的传入数据包的数量:" & IPIS.IncomingUnknownProtocolPackets & vbCrLf
TextBox1.Text &= "接收的非单播数据包个数:" & IPIS.NonUnicastPacketsReceived & vbCrLf
TextBox1.Text &= "发送的非单播数据包个数:" & IPIS.NonUnicastPacketsSent & vbCrLf
TextBox1.Text &= "丢弃的传出数据包数:" & IPIS.OutgoingPacketsDiscarded & vbCrLf
TextBox1.Text &= "有错误的传出数据包数:" & IPIS.OutgoingPacketsWithErrors & vbCrLf
TextBox1.Text &= "输出队列的长度:" & IPIS.OutputQueueLength & vbCrLf
TextBox1.Text &= "接收到的单播数据包个数:" & IPIS.UnicastPacketsReceived & vbCrLf
TextBox1.Text &= "发送的单播数据包个数:" & IPIS.UnicastPacketsSent & vbCrLf
TextBox1.Text &= "==========================================================================" & vbCrLf
Next
End Sub
运行时如下:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录