Visual Basic Code Snippet - Get local computer IP address

Visual Basic Code Snippet - Get local computer IP address

Visual Basic Code Snippet - Get local computer IP address

(VB) Visual Basic code snippet get the IP address from the local machine. For computers with multiple IP addresses with one or more NIC cards can use IP address object list to obtain all the IP addresses.

Bookmark:

Visual Basic Code Snippet - Get local computer IP address

This .Net Visual Basic code snippet get the IP address from the local machine. This function uses System.Net namespace to get local machine hostname and then get a array of IPAddress objects. Modify the exception handling section to meet your project requirements.

''' <summary>
''' Gets IP addresses of the local computer
''' </summary>
Public Function GetLocalIP() As String
    Dim _IP As String = Nothing

    ' Resolves a host name or IP address to an IPHostEntry instance.
    ' IPHostEntry - Provides a container class for Internet host address information. 
    Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())

    ' IPAddress class contains the address of a computer on an IP network. 
    For Each _IPAddress As System.Net.IPAddress In _IPHostEntry.AddressList
        ' InterNetwork indicates that an IP version 4 address is expected 
        ' when a Socket connects to an endpoint
        If _IPAddress.AddressFamily.ToString() = "InterNetwork" Then
            _IP = _IPAddress.ToString()
        End If
    Next _IPAddress
    Return _IP
End Function


Here is a simple example showing how to use above function (GetLocalIP) to display local machine IP address.

Console.WriteLine("Local computer IP address : " & GetLocalIP())


This .Net code snippet gets the array of all the IPAddress objects. Use this method if you are writing sockets applications, a computer can have multiple IP addresses with one or more NIC cards.

''' <summary>
''' Gets IP addresses object list from the local computer
''' </summary>
Public Function GetLocalIPList() As System.Net.IPAddress()
    ' Resolves a host name or IP address to an IPHostEntry instance.
    ' IPHostEntry - Provides a container class for Internet host address information. 
    Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())

    Return _IPHostEntry.AddressList
End Function


Here is a simple example showing how to use above function (GetLocalIPList) to display all the IP addresses from local machine.

' display all IP addresses of local machine
For Each _IPAddress As System.Net.IPAddress In GetLocalIPList()
    Console.WriteLine(_IPAddress.ToString())
Next _IPAddress


' display all IP addresses of local machine with addressing scheme
For Each _IPAddress As System.Net.IPAddress In GetLocalIPList()
    Console.WriteLine(_IPAddress.AddressFamily.ToString() & " = " & _IPAddress.ToString())
Next _IPAddress


VB Keywords Used:

  • DNS
  • IPAddress
  • GetHostEntry
  • IPHostEntry
  • AddressList
  • AddressFamily
  • InterNetwork
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, DNS, Domain Name System, IPAddress, GetHostEntry, IPHostEntry, AddressFamily, InterNetwork, Socket programming, Networking, Network monitoring
  • Programming Language : Visual Basic (VB)

External Resources:

José :: August 21-2011 :: 03:45 AM

This code does not works on router.

Leave a comment