Question Getting a NIC Name, IP (v4) and Connection Status

XardozCom

Member
Joined
May 10, 2013
Messages
16
Location
Washington State USA
Programming Experience
10+
We have been chasing this for a week.
The Internet is full of half answers.

With this code modified, can we get the Adapter Name (Not Manufacture), IPv4 and Connection status?

VB.NET:
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface
        For Each adapter In adapters
            Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
            Console.WriteLine(adapter.Description)
            Console.WriteLine("  DNS suffix................................. :{0}", properties.DnsSuffix)
            Console.WriteLine("  DNS enabled ............................. : {0}", properties.IsDnsEnabled)
            Console.WriteLine("  Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled)
            Console.WriteLine(properties.GetIPv4Properties())
        Next adapter


        For Each nic As System.Net.NetworkInformation.NetworkInterface In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
            Console.WriteLine(nic.GetIPProperties().UnicastAddresses(0).Address)
        Next
 
Close, but not!

This work unless you have one or more NICs not connected


VB.NET:
Imports System.NetImports System.Net.NetworkInformation


Public Class Form1
    Public sFormat As String = "{0,-40}{1,20}"


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        With rtb


            .Clear()


            Dim networks() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
            Dim props() As IPInterfaceProperties = networks.Select(Function(net) net.GetIPProperties()).ToArray()
            Dim addys() As IPAddress = props.SelectMany(Function(prop) prop.UnicastAddresses.Select(Function(a) a.Address)).ToArray()


            'if they are equal then everything worked right and they're in order
            If addys.GetUpperBound(0) = networks.GetUpperBound(0) Then
                For i As Integer = networks.GetLowerBound(0) To networks.GetUpperBound(0)
                    .AppendText(String.Format(sFormat, networks(i).Name.ToString, addys(i).ToString) & vbNewLine)
                Next
            End If
            .AppendText("==============================================" & vbNewLine)
            Dim ary() As String = GetAssignedIPAddresses()
            For i As Integer = ary.GetLowerBound(0) To ary.GetUpperBound(0)
                Try
                    .AppendText(ary(i).ToString & vbNewLine)
                Catch ex As Exception


                End Try


            Next


        End With
    End Sub


    Public Function GetAssignedIPAddresses() As String() ' IPAddress()
        Dim networks() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
        Dim props() As IPInterfaceProperties = networks.Select(Function(net) net.GetIPProperties()).ToArray()
        Dim addys() As IPAddress = props.SelectMany(Function(prop) prop.UnicastAddresses.Select(Function(a) a.Address)).ToArray()


        'this only returns the ip addresses, not the 'connection name'
        'Return addys.Where(Function(a) Not IPAddress.IsLoopback(a)).ToArray()




        Dim netary(addys.GetUpperBound(0)) As String
        For i As Integer = addys.GetLowerBound(0) To addys.GetUpperBound(0)
            Try
                netary(i) = networks(i).Name.ToString & ", " & addys(i).ToString
            Catch ex As Exception


            End Try


        Next
        GetAssignedIPAddresses = netary
    End Function


End Class
 

Similar threads

Back
Top