获取当前 NIC 设置
Get current NIC settings
所以,长话短说,我目前无法弄清楚的代码部分旨在报告给定 NIC 的当前 IP 设置,所以我希望它基本上吐出 IP,子网和当前设置的默认网关。我有一个有效的解决方案,但它似乎只在 NIC 设置为 DHCP 时表现不错,这对我的应用程序不利。
这是我当前的代码:
Public Sub NetGet()
MainForm.NetLabelIP.Text = "IPv4 Address: "
MainForm.NetLabelIP.Text = "subnet Mask: "
MainForm.NetLabelIP.Text = "Default Gateway: "
MainForm.NetLabelCN.Text = "Computer Name: " + System.Net.Dns.GetHostName()
For Each ip In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList
If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
'IPv4 Adress
MainForm.NetLabelIP.Text = "IPv4 Address: " + ip.ToString()
For Each adapter As Net.NetworkInformation.NetworkInterface In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
If adapter.Name = MainForm.interfaceSelector.SelectedItem Then
For Each unicastIPAddressInformation As Net.NetworkInformation.UnicastIPAddressInformation In adapter.GetIPProperties().UnicastAddresses
If unicastIPAddressInformation.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
If ip.Equals(unicastIPAddressInformation.Address) Then
'Subnet Mask
MainForm.NetLabelSM.Text = "Subnet Mask: " + unicastIPAddressInformation.IPv4Mask.ToString()
Dim adapterProperties As Net.NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
For Each gateway As Net.NetworkInformation.GatewayIPAddressInformation In adapterProperties.GatewayAddresses
'Default Gateway
MainForm.NetLabelDG.Text = "Default Gateway: " + gateway.Address.ToString()
Next
If unicastIPAddressInformation.PrefixOrigin = 3 Then
DHCP = True
MainForm.NetLabelDHCP.Text = "DHCP Enabled: TRUE"
Else
DHCP = False
MainForm.NetLabelDHCP.Text = "DHCP Enabled: FALSE"
End If
''DNS1
'if adapterproperties.dnsaddresses.count > 0 then
' label5.text = adapterproperties.dnsaddresses(0).tostring()
'end if
''DNS2
'if adapterproperties.dnsaddresses.count > 1 then
' label6.text = adapterproperties.dnsaddresses(1).tostring()
'end if
End If
End If
Next
End If
Next
End If
Next
End Sub
我假设事后看来这会是一件非常愚蠢的事情,但是我觉得最好与社区分享我的请求,这样任何其他寻找类似解决方案的人都可以在这里找到他们的答案。
提前致谢各位。
此 NetInterfacesInfo
class 实现两个静态(共享)方法 return 机器网络接口的信息:
NetInterfacesInfo.GetNetworkInterfaces()
此方法return所有支持 IPV4 的网络接口,但环回接口除外。
信息在 List(Of NetWorkInterfacesInfo)
中 returned,它公开了这些属性:
ConnectionName
:分配给连接的名称(Local Area Network (LAN)
)
Description
: 接口的友好名称
IPV4Addresses
:每个 IP 地址的简化列表作为字符串,相关的网络掩码和默认网关。
IpAddresses
:与网络接口关联的 IP 地址列表。
DHCPSservers
:与网络接口关联的 DHCP 服务器列表。
DnsServers
:与网络接口关联的 DNS 服务器列表。
Gateways
:与网络接口关联的网关地址列表。
IsDHCPEnabled
:指定 IP 地址是由 DHCP 服务器提供还是静态地址。
MacAddress
:网卡的MAC地址
Status
:接口功能正常(向上)或不正常(向下)
InterfaceType
:接口类型。该值可以是多种可能的接口类型之一:Wireless80211
、Tunnel
、 FastEthernetFx
等
IPV4Addresses
属性return 与网络接口关联的 IP 地址的简化列表。这些信息包含在 IpV4AddressInfo
class 中,它提供了这些属性:
IpAddress
:IP 地址的字符串表示形式。
NetMask
:IP 地址的网络掩码的字符串表示。
DefaultGateway
:默认网关地址的字符串表示形式。
IsDnsEligible
:指定IP地址可以出现在DNS中(可路由)
示例用法:
Dim allNicsInfo = NetInterfacesInfo.GetNetworkInterfaces()
For Each nic As NetInterfacesInfo.NetWorkInterfacesInfo In allNicsInfo
Console.WriteLine($"Description: {nic.Description} Type: {nic.InterfaceType}")
Next
Dim Wireless = allNicsInfo.Where(Function(nic) nic.InterfaceType = NetworkInterfaceType.Wireless80211)
NetInterfacesInfo.IpV4AddressSimpleList
与网络接口关联的 IP 地址的简化列表也可以使用此静态(共享)方法检索,提供 Name(实际上,Description
属性) 网络接口。
此方法 returns a List(Of IpV4AddressInfo)
,一个简化的、只有字符串的版本,每个 IP 地址(指定网络接口的)、它的网络掩码和相关的默认网关.
示例用法:
Dim nicInfo = NetInterfacesInfo.IpV4AddressSimpleList("Some NIC Name")
For Each ipV4Addr As NetInterfacesInfo.IpV4AddressInfo In nicInfo
Console.WriteLine(ipV4Addr.IpAddress)
Console.WriteLine(ipV4Addr.NetMask)
Console.WriteLine(ipV4Addr.DefaultGateway)
Next
将主要 class 附加到 ComboBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim allNicsInfo = NetInterfacesInfo.GetNetworkInterfaces()
ComboBox1.DisplayMember = "ConnectionName"
ComboBox1.DataSource = allNicsInfo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim cbo = DirectCast(sender, ComboBox)
If cbo.SelectedIndex = -1 Then Return
Dim nicInfo = DirectCast(cbo.SelectedItem, NetInterfacesInfo.NetWorkInterfacesInfo)
TextBox1.Text = String.Join(Environment.NewLine, nicInfo.IPV4Addresses.
Select(Function(nic) $"IP Address: {nic.IpAddress} NetMask: {nic.NetMask}"))
TextBox2.Text = nicInfo.IPV4Addresses.First().DefaultGateway
End Sub
Imports System.Linq
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Public Class NetInterfacesInfo
Public Shared Function GetNetworkInterfaces() As List(Of NetWorkInterfacesInfo)
Dim ifInfo As New List(Of NetWorkInterfacesInfo)()
ifInfo.AddRange(GetNetworInterfaces().
Where(Function(nic) nic.NetworkInterfaceType <> NetworkInterfaceType.Loopback AndAlso
nic.Supports(NetworkInterfaceComponent.IPv4)).
Select(Function(nic) New NetWorkInterfacesInfo() With {
.Description = nic.Description,
.ConnectionName = nic.Name,
.IsDHCPEnabled = nic.GetIPProperties().GetIPv4Properties().IsDhcpEnabled,
.DHCPSservers = nic.GetIPProperties().DhcpServerAddresses.ToList(),
.DnsServers = nic.GetIPProperties().DnsAddresses.ToList(),
.Gateways = nic.GetIPProperties().GatewayAddresses.Select(Function(ipa) ipa.Address).ToList(),
.InterfaceType = nic.NetworkInterfaceType,
.IpAddresses = nic.GetIPProperties().UnicastAddresses.Select(Function(ipa) ipa.Address).ToList(),
.MacAddress = nic.GetPhysicalAddress().GetAddressBytes().
Select(Function(b) b.ToString("X")).Aggregate(Function(s1, s2) s2 + ":" + s1),
.Status = nic.OperationalStatus,
.IPV4Addresses = GetIpV4AddressInfo(nic)
}))
Return ifInfo
End Function
Public Shared Function IpV4AddressSimpleList(nicName As String) As List(Of IpV4AddressInfo)
Dim nic = GetNetworInterfaceByName(nicName)
If nic Is Nothing Then Return Nothing
Return nic.GetIPProperties().UnicastAddresses.
Where(Function(ipa) ipa.Address.AddressFamily = AddressFamily.InterNetwork).
Select(Function(ipa) New IpV4AddressInfo() With {
.IpAddress = ipa.Address?.ToString(),
.NetMask = ipa.IPv4Mask?.ToString(),
.IsDnsEligible = ipa.IsDnsEligible,
.DefaultGateway = nic.GetIPProperties().GatewayAddresses.FirstOrDefault()?.Address?.ToString()
}).ToList()
End Function
Private Shared Function GetIpV4AddressInfo(nic As NetworkInterface) As List(Of IpV4AddressInfo)
Return nic.GetIPProperties().UnicastAddresses.
Where(Function(ipa) ipa.Address.AddressFamily = AddressFamily.InterNetwork).
Select(Function(ipa) New IpV4AddressInfo() With {
.IpAddress = ipa.Address?.ToString(),
.NetMask = ipa.IPv4Mask?.ToString(),
.IsDnsEligible = ipa.IsDnsEligible,
.DefaultGateway = nic.GetIPProperties().GatewayAddresses.FirstOrDefault()?.Address?.ToString()
}).ToList()
End Function
Private Shared Function GetNetworInterfaces() As NetworkInterface()
Return NetworkInterface.GetAllNetworkInterfaces()
End Function
Private Shared Function GetNetworInterfaceByName(nicName As String) As NetworkInterface
Return NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(Function(nic) nic.Name = nicName)
End Function
Public Class NetWorkInterfacesInfo
Public Property ConnectionName As String
Public Property Description As String
Public Property IPV4Addresses As List(Of IpV4AddressInfo)
Public Property IpAddresses As List(Of IPAddress)
Public Property DHCPSservers As List(Of IPAddress)
Public Property DnsServers As List(Of IPAddress)
Public Property Gateways As List(Of IPAddress)
Public Property IsDHCPEnabled As Boolean
Public Property MacAddress As String
Public Property Status As OperationalStatus
Public Property InterfaceType As NetworkInterfaceType
End Class
Public Class IpV4AddressInfo
Public Property IpAddress As String
Public Property NetMask As String
Public Property DefaultGateway As String
Public Property IsDnsEligible As Boolean
End Class
End Class
所以,长话短说,我目前无法弄清楚的代码部分旨在报告给定 NIC 的当前 IP 设置,所以我希望它基本上吐出 IP,子网和当前设置的默认网关。我有一个有效的解决方案,但它似乎只在 NIC 设置为 DHCP 时表现不错,这对我的应用程序不利。
这是我当前的代码:
Public Sub NetGet()
MainForm.NetLabelIP.Text = "IPv4 Address: "
MainForm.NetLabelIP.Text = "subnet Mask: "
MainForm.NetLabelIP.Text = "Default Gateway: "
MainForm.NetLabelCN.Text = "Computer Name: " + System.Net.Dns.GetHostName()
For Each ip In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList
If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
'IPv4 Adress
MainForm.NetLabelIP.Text = "IPv4 Address: " + ip.ToString()
For Each adapter As Net.NetworkInformation.NetworkInterface In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
If adapter.Name = MainForm.interfaceSelector.SelectedItem Then
For Each unicastIPAddressInformation As Net.NetworkInformation.UnicastIPAddressInformation In adapter.GetIPProperties().UnicastAddresses
If unicastIPAddressInformation.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
If ip.Equals(unicastIPAddressInformation.Address) Then
'Subnet Mask
MainForm.NetLabelSM.Text = "Subnet Mask: " + unicastIPAddressInformation.IPv4Mask.ToString()
Dim adapterProperties As Net.NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
For Each gateway As Net.NetworkInformation.GatewayIPAddressInformation In adapterProperties.GatewayAddresses
'Default Gateway
MainForm.NetLabelDG.Text = "Default Gateway: " + gateway.Address.ToString()
Next
If unicastIPAddressInformation.PrefixOrigin = 3 Then
DHCP = True
MainForm.NetLabelDHCP.Text = "DHCP Enabled: TRUE"
Else
DHCP = False
MainForm.NetLabelDHCP.Text = "DHCP Enabled: FALSE"
End If
''DNS1
'if adapterproperties.dnsaddresses.count > 0 then
' label5.text = adapterproperties.dnsaddresses(0).tostring()
'end if
''DNS2
'if adapterproperties.dnsaddresses.count > 1 then
' label6.text = adapterproperties.dnsaddresses(1).tostring()
'end if
End If
End If
Next
End If
Next
End If
Next
End Sub
我假设事后看来这会是一件非常愚蠢的事情,但是我觉得最好与社区分享我的请求,这样任何其他寻找类似解决方案的人都可以在这里找到他们的答案。
提前致谢各位。
此 NetInterfacesInfo
class 实现两个静态(共享)方法 return 机器网络接口的信息:
NetInterfacesInfo.GetNetworkInterfaces()
此方法return所有支持 IPV4 的网络接口,但环回接口除外。
信息在 List(Of NetWorkInterfacesInfo)
中 returned,它公开了这些属性:
ConnectionName
:分配给连接的名称(Local Area Network (LAN)
)Description
: 接口的友好名称IPV4Addresses
:每个 IP 地址的简化列表作为字符串,相关的网络掩码和默认网关。IpAddresses
:与网络接口关联的 IP 地址列表。DHCPSservers
:与网络接口关联的 DHCP 服务器列表。DnsServers
:与网络接口关联的 DNS 服务器列表。Gateways
:与网络接口关联的网关地址列表。IsDHCPEnabled
:指定 IP 地址是由 DHCP 服务器提供还是静态地址。MacAddress
:网卡的MAC地址Status
:接口功能正常(向上)或不正常(向下)InterfaceType
:接口类型。该值可以是多种可能的接口类型之一:Wireless80211
、Tunnel
、FastEthernetFx
等
IPV4Addresses
属性return 与网络接口关联的 IP 地址的简化列表。这些信息包含在 IpV4AddressInfo
class 中,它提供了这些属性:
IpAddress
:IP 地址的字符串表示形式。NetMask
:IP 地址的网络掩码的字符串表示。DefaultGateway
:默认网关地址的字符串表示形式。IsDnsEligible
:指定IP地址可以出现在DNS中(可路由)
示例用法:
Dim allNicsInfo = NetInterfacesInfo.GetNetworkInterfaces()
For Each nic As NetInterfacesInfo.NetWorkInterfacesInfo In allNicsInfo
Console.WriteLine($"Description: {nic.Description} Type: {nic.InterfaceType}")
Next
Dim Wireless = allNicsInfo.Where(Function(nic) nic.InterfaceType = NetworkInterfaceType.Wireless80211)
NetInterfacesInfo.IpV4AddressSimpleList
与网络接口关联的 IP 地址的简化列表也可以使用此静态(共享)方法检索,提供 Name(实际上,Description
属性) 网络接口。
此方法 returns a List(Of IpV4AddressInfo)
,一个简化的、只有字符串的版本,每个 IP 地址(指定网络接口的)、它的网络掩码和相关的默认网关.
示例用法:
Dim nicInfo = NetInterfacesInfo.IpV4AddressSimpleList("Some NIC Name")
For Each ipV4Addr As NetInterfacesInfo.IpV4AddressInfo In nicInfo
Console.WriteLine(ipV4Addr.IpAddress)
Console.WriteLine(ipV4Addr.NetMask)
Console.WriteLine(ipV4Addr.DefaultGateway)
Next
将主要 class 附加到 ComboBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim allNicsInfo = NetInterfacesInfo.GetNetworkInterfaces()
ComboBox1.DisplayMember = "ConnectionName"
ComboBox1.DataSource = allNicsInfo
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim cbo = DirectCast(sender, ComboBox)
If cbo.SelectedIndex = -1 Then Return
Dim nicInfo = DirectCast(cbo.SelectedItem, NetInterfacesInfo.NetWorkInterfacesInfo)
TextBox1.Text = String.Join(Environment.NewLine, nicInfo.IPV4Addresses.
Select(Function(nic) $"IP Address: {nic.IpAddress} NetMask: {nic.NetMask}"))
TextBox2.Text = nicInfo.IPV4Addresses.First().DefaultGateway
End Sub
Imports System.Linq
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Public Class NetInterfacesInfo
Public Shared Function GetNetworkInterfaces() As List(Of NetWorkInterfacesInfo)
Dim ifInfo As New List(Of NetWorkInterfacesInfo)()
ifInfo.AddRange(GetNetworInterfaces().
Where(Function(nic) nic.NetworkInterfaceType <> NetworkInterfaceType.Loopback AndAlso
nic.Supports(NetworkInterfaceComponent.IPv4)).
Select(Function(nic) New NetWorkInterfacesInfo() With {
.Description = nic.Description,
.ConnectionName = nic.Name,
.IsDHCPEnabled = nic.GetIPProperties().GetIPv4Properties().IsDhcpEnabled,
.DHCPSservers = nic.GetIPProperties().DhcpServerAddresses.ToList(),
.DnsServers = nic.GetIPProperties().DnsAddresses.ToList(),
.Gateways = nic.GetIPProperties().GatewayAddresses.Select(Function(ipa) ipa.Address).ToList(),
.InterfaceType = nic.NetworkInterfaceType,
.IpAddresses = nic.GetIPProperties().UnicastAddresses.Select(Function(ipa) ipa.Address).ToList(),
.MacAddress = nic.GetPhysicalAddress().GetAddressBytes().
Select(Function(b) b.ToString("X")).Aggregate(Function(s1, s2) s2 + ":" + s1),
.Status = nic.OperationalStatus,
.IPV4Addresses = GetIpV4AddressInfo(nic)
}))
Return ifInfo
End Function
Public Shared Function IpV4AddressSimpleList(nicName As String) As List(Of IpV4AddressInfo)
Dim nic = GetNetworInterfaceByName(nicName)
If nic Is Nothing Then Return Nothing
Return nic.GetIPProperties().UnicastAddresses.
Where(Function(ipa) ipa.Address.AddressFamily = AddressFamily.InterNetwork).
Select(Function(ipa) New IpV4AddressInfo() With {
.IpAddress = ipa.Address?.ToString(),
.NetMask = ipa.IPv4Mask?.ToString(),
.IsDnsEligible = ipa.IsDnsEligible,
.DefaultGateway = nic.GetIPProperties().GatewayAddresses.FirstOrDefault()?.Address?.ToString()
}).ToList()
End Function
Private Shared Function GetIpV4AddressInfo(nic As NetworkInterface) As List(Of IpV4AddressInfo)
Return nic.GetIPProperties().UnicastAddresses.
Where(Function(ipa) ipa.Address.AddressFamily = AddressFamily.InterNetwork).
Select(Function(ipa) New IpV4AddressInfo() With {
.IpAddress = ipa.Address?.ToString(),
.NetMask = ipa.IPv4Mask?.ToString(),
.IsDnsEligible = ipa.IsDnsEligible,
.DefaultGateway = nic.GetIPProperties().GatewayAddresses.FirstOrDefault()?.Address?.ToString()
}).ToList()
End Function
Private Shared Function GetNetworInterfaces() As NetworkInterface()
Return NetworkInterface.GetAllNetworkInterfaces()
End Function
Private Shared Function GetNetworInterfaceByName(nicName As String) As NetworkInterface
Return NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(Function(nic) nic.Name = nicName)
End Function
Public Class NetWorkInterfacesInfo
Public Property ConnectionName As String
Public Property Description As String
Public Property IPV4Addresses As List(Of IpV4AddressInfo)
Public Property IpAddresses As List(Of IPAddress)
Public Property DHCPSservers As List(Of IPAddress)
Public Property DnsServers As List(Of IPAddress)
Public Property Gateways As List(Of IPAddress)
Public Property IsDHCPEnabled As Boolean
Public Property MacAddress As String
Public Property Status As OperationalStatus
Public Property InterfaceType As NetworkInterfaceType
End Class
Public Class IpV4AddressInfo
Public Property IpAddress As String
Public Property NetMask As String
Public Property DefaultGateway As String
Public Property IsDnsEligible As Boolean
End Class
End Class