-
vb.net 教程 7-1 本地网络信息的获取 2 本地网络接口信息2
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
简要说明一下如何在代码中使用上述类的属性及方法
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 教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/UruseiBest/article/details/81811451
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数