Identify Default Network Card – Visual Basic

I’ve been trying to get back into writing some code, so I started with a little project that may see the light of the day soon.

With some help from some examples out on the internet, I came up with this function, which is now part of a class in my project, that tries to identify the “default network card” on your computer. Once the default network card is identified, the function will return: interface GUI id, interface name, interface description (if any), interface status, interface speed, MAC address, ip address, subnet, gateway, DNS, MTU, DHCP and WINS information.

I tested this code on a few computers, as well on some virtual machines, and it seems to work well. I’d love to hear some feedback about the code, and perhaps get a few coding pointers. (Screenshot at the bottom is from a virtual machine running under Parallels)

Here’s the code.

Private Function GetPrimaryNic()

‘ DESCRIPTION: this function  will provide networking details for primary network card

 

Dim PrimaryNic As New Collection

 

For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces

 

‘ Find network cards with gateway information (this may show more than one network card depending on computer)

For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses

 

‘ if gateway address is NOT 0.0.0.0 and the network card status is UP then we’ve found the main network card

If gatewayAddr.Address.ToString “0.0.0.0” And networkCard.OperationalStatus.ToString() = “Up” Then

PrimaryNic.Add(“Interface GUID: “ & networkCard.Id)

PrimaryNic.Add(“Name:”.PadRight(15) & networkCard.Name)

PrimaryNic.Add(“Description:”.PadRight(15) & networkCard.Description)

PrimaryNic.Add(“Status:”.PadRight(15) & networkCard.OperationalStatus.ToString)

PrimaryNic.Add(“Speed:”.PadRight(15) & (networkCard.Speed / 1000000).ToString(“#,000”) & ” Mbps”)

PrimaryNic.Add(“MAC Address:”.PadRight(15) & networkCard.GetPhysicalAddress.ToString)

 

‘ Get IP Address(es) and subnet(s) information

Dim IpAddressAndSubnet As UnicastIPAddressInformation

 

For Each IpAddressAndSubnet In networkCard.GetIPProperties.UnicastAddresses

PrimaryNic.Add(”  IP Address:”.PadRight(15) & IpAddressAndSubnet.Address.ToString)

PrimaryNic.Add(”  Subnet:”.PadRight(15) & IpAddressAndSubnet.IPv4Mask.ToString)

Next

 

‘ Get IP gateway information

PrimaryNic.Add(”  Gateway:”.PadRight(15) & gatewayAddr.Address.ToString)

 

‘ Get IP DNS information

Dim DnsAddress As IPAddress

 

For Each DnsAddress In networkCard.GetIPProperties.DnsAddresses

PrimaryNic.Add(“DNS entry:”.PadRight(15) & DnsAddress.ToString)

Next

 

‘ Other IP information

Dim IPProp As IPInterfaceProperties = networkCard.GetIPProperties

 

If Not IPProp Is Nothing Then

PrimaryNic.Add(“DNS Enabled:”.PadRight(15) & IPProp.IsDnsEnabled.ToString)

PrimaryNic.Add(“Dynamic DNS:”.PadRight(15) & IPProp.IsDynamicDnsEnabled.ToString)

End If

 

Dim IPv4 As IPv4InterfaceProperties = networkCard.GetIPProperties.GetIPv4Properties

 

If Not IPv4 Is Nothing Then

PrimaryNic.Add(“DHCP Enabled:”.PadRight(15) & IPv4.IsDhcpEnabled.ToString)

PrimaryNic.Add(“MTU Setting:”.PadRight(15) & IPv4.Mtu.ToString)

PrimaryNic.Add(“Uses WINS:”.PadRight(15) & IPv4.UsesWins.ToString)

End If

 

End If

Next

Next

 

Return PrimaryNic

 

End Function

 

GetPrimaryNic

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: