获取当前的NIC设置(VB.Net)

时间:2019-02-20 09:14:00

标签: vb.net networking

因此,长话短说,我目前无法弄清的代码部分旨在报告给定NIC的当前IP设置,因此我希望它实质上是吐出IP,子网和默认值当前设置的网关。我有一个解决方案,但似乎只有在将NIC设置为DHCP时,它才能很好地运行,这对我的应用程序不利。

这是我当前的功能(实际上不是我所知道的功能,但idk还有什么可称呼的。)

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

我认为事后看来这将是令人发指的愚蠢之举,但是我觉得最好与社区分享我的请求,以便其他寻求类似解决方案的人都可以在这里找到答案。

谢谢,伙计们。

1 个答案:

答案 0 :(得分:0)

NetInterfacesInfo 类实现了两个静态(共享)方法,这些方法在计算机的网络接口上返回信息:

  1. NetInterfacesInfo.GetNetworkInterfaces()

此方法返回除Loopback接口之外的所有支持IPV4的网络接口。 信息以 List(Of NetWorkInterfacesInfo) 返回,其中包含以下属性:

  • ConnectionName :分配给连接的名称(Local Area Network (LAN)
  • Description :接口的原始名称
  • IPV4Addresses :每个IP地址(以字符串形式),关联的NetMask和默认网关的简化列表。
  • IpAddresses :与网络接口关联的IP地址的列表。
  • DHCPSservers :与网络接口关联的DHCP服务器的列表。
  • DnsServers :与网络接口关联的DNS服务器的列表。
  • Gateways :与网络接口关联的网关地址列表。
  • IsDHCPEnabled :指定IP地址是由DHCP服务器提供还是静态地址。
  • MacAddress :NIC的MAC地址
  • Status :该界面正常(打开)或不正常(向下)
  • InterfaceType :接口的类型。此值可以是许多可能的接口类型之一: Wireless80211 Tunnel FastEthernetFx 等等

IPV4Addresses 属性返回与网络接口关联的IP地址的简化列表。这些信息包含在 IpV4AddressInfo 类中,该类提供以下属性:

  • IpAddress :IP地址的字符串表示形式。
  • NetMask :Ip地址的NetMask的字符串表示形式。
  • 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)

  1. NetInterfacesInfo.IpV4AddressSimpleList

还可以使用此静态(共享)方法检索与网络接口关联的IP地址的简化列表,提供网络的 Name (实际上是Description属性)界面。
此方法返回一个 List(Of IpV4AddressInfo) ,它是每个IP地址(指定的网络接口),其NetMask和关联的默认网关的简化的,仅字符串形式的版本。

样品用量:

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

将主类附加到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