一、IPAddress 类
IPAddress类在System.NET命名空间中,它只能单纯地用来保存一般地址信息。
System.Net.NetworkInformation命名空间中还有几种IP地址有关的类,用来保存特定类型地址的相关信息,如:
相关类:
IPAddressCollection 保存一组IPAddress类
IPAddressInformation 提供网络接口地址的相关信息
IPAddressInformationCollection 保存一组IPAddressInformation 类
GatewayIPAddressInformation 表示网络网关的Ip地址
GatewayIPAddressInformationCollection 保存一组网络网关的ip地址
MulticastIPAddressInformation 提供网络接口多播地址的相关信息
MulticastIPAddressInformationCollection 保存一组多播地址信息的类
UnicastIPAddressInformation 提供网络接口单点传送地址的相关信息
UnicastIPAddressInformationCollection 保存一组单点传送信息的类
上面类实体对象可由IPInterfaceProperties对象(Networklnterface.GetIPProperties)的属性成员取得。
Public MustOverride Function GetIPProperties As IPInterfaceProperties对于IPv4及IPv6还可用GetIPv4Properties与GetIPv6Properties类。
要取得这两个类,可用IPInterfaceProperties对象的GetIPv4Properties()与GetIPv6Properties()方法:
Public MustOverride Function GetIPv4Properties As IPv4InterfaceProperties Public MustOverride Function GetIPv6Properties As IPv6InterfaceProperties
二、IPInterfaceProperties 类
该类提供IPv4或IPv6的网络接口相关信息。
IPInterfaceProperties是抽象类,可直接用Networklnterface的GetIPProperties()方法所返回的实体对象进行操作,其属性可取得因特网通信协议的相关信息。
属性:
AnycastAddresses 获取分配给此接口的任意广播IP地址。
DhcpServerAddresses 获取此接口的动态主机配置协议(DHCP)服务器的地址。
DnsAddresses 获取此接口的域名系统(DNS)服务器的地址。
DnsSuffix 获取与此接口关联的域名系统 (DNS) 后缀。
GatewayAddresses 获取此接口的IPv4网关地址。
IsDnsEnabled 获取一个Boolean值,该值指示是否将NetBt配置为对此接口使用DNS名称解析。
IsDynamicDnsEnabled 获取Boolean值,该值指示此接口是否被配置为自动向域名系统(DNS)注册其IP地址信息。
MulticastAddresses 获取分配给此接口的多路广播地址。
UnicastAddresses 获取分配给此接口的单播地址。
WinsServersAddresses 获取Windows Internet名称服务(WINS 服务器的地址。
Imports System.Net.NetworkInformation Imports System.Net Public Class Form1 Private Sub btnInterface_Click(sender As Object,e As EventArgs) Handles btnInterface.Click Dim adapter As NetworkInterface Dim adapters As NetworkInterface() Dim strInfo As String = "" Dim intNum As Int32 adapters = NetworkInterface.GetAllNetworkInterfaces For Each adapter In adapters intNum += 1 strInfo &= "网络接口 " & intNum.ToString & vbCrLf strInfo &= "--------------------" & vbCrLf Dim ipInfo As IPInterfaceProperties = adapter.GetIPProperties '接口IP属性 strInfo &= "是否DNS解析:" & ipInfo.IsDnsEnabled.ToString & vbCrLf strInfo &= "是否动态IP:" & ipInfo.IsDynamicDnsEnabled.ToString & vbCrLf strInfo &= "DNS后缀:" & ipInfo.DnsSuffix & vbCrLf Dim addrs As IPAddressCollection = ipInfo.DnsAddresses For Each addr As IPAddress In addrs strInfo &= "DNS地址:" & addr.MapToIPv4.ToString & vbCrLf 'IP地址转为标准表示法 Next Dim casts As IPAddressInformationCollection = ipInfo.AnycastAddresses For Each cast As IPAddressInformation In casts strInfo &= "任意广播地址:" & cast.Address.ToString() & vbCrLf strInfo &= "在DNS中是否有效:" & cast.IsDnsEligible.ToString & vbCrLf strInfo &= "是否瞬态(群集):" & cast.IsTransient.ToString & vbCrLf Next Dim gates As GatewayIPAddressInformationCollection = ipInfo.GatewayAddresses For Each gate As GatewayIPAddressInformation In gates strInfo &= "网关地址:" & gate.Address.ToString() & vbCrLf Next Dim mults As MulticastIPAddressInformationCollection = ipInfo.MulticastAddresses For Each mult As MulticastIPAddressInformation In mults strInfo &= "多播地址:" & mult.Address.ToString() & vbCrLf Next Dim uncs As UnicastIPAddressInformationCollection = ipInfo.UnicastAddresses For Each unc As UnicastIPAddressInformation In uncs strInfo &= "单点传送地址:" & unc.Address.ToString() & vbCrLf Next Dim wins As IPAddressCollection = ipInfo.WinsServersAddresses For Each win As IPAddress In wins strInfo &= "wins服务器:" & win.ToString() & vbCrLf Next strInfo &= vbCrLf & vbCrLf Next txtInterface.Text = strInfo End Sub End Class
三、IPGlobalProperties 类
本地计算机的网络连接的信息。
IPGlobalProperties为抽象类,可通过NetworkInterface取得IPGlobalProperties对象,或直接引用其GetIPGIobalProperties()方法取得对象:
Public Shared Function GetIPGlobalProperties As IPGlobalProperties例:
Imports System.Net.NetworkInformation Dim a As IPGlobalProperties a.GetIPv4GlobalStatistics属性:
DhcpScopeName 获取动态主机配置协议 (DHCP) 范围名
DomainName 获取在其中注册本地计算机的域。
HostName 获取本地计算机的主机名。
IsWinsProxy 获取 Boolean 值,指定本地是否作为 Windows Internet 名称服务 (WINS) 代理。
NodeType 获取本地计算机的网络基本输入/输出系统 (NetBIOS) 节点类型。
方法:
GetIPv4GlobalStatistics提供IPv4统计数据。
GetIPv6GlobalStatistics提供IPv6统计数据。
Imports System.Net.NetworkInformation Public Class Form1 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim a As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties Dim b As IPGlobalStatistics = a.GetIPv4GlobalStatistics Dim s As String = "" s &= "主机名:" & a.HostName & vbCrLf s &= "DHCP范围:" & a.DhcpScopeName.ToString & vbCrLf s &= "所在域:" & a.DomainName.ToString & vbCrLf s &= "是否代理:" & a.IsWinsProxy.ToString & vbCrLf s &= "节点类型:" & a.NodeType.ToString & vbCrLf s &= "收到数据包:" & b.ReceivedPackets.ToString & vbCrLf s &= "转发包:" & b.ReceivedPacketsForwarded.ToString & vbCrLf s &= "传送数据包:" & b.ReceivedPacketsDelivered.ToString & vbCrLf s &= "丢弃数据包:" & b.ReceivedPacketsDiscarded.ToString & vbCrLf TextBox1.Text = s End Sub End Class