Article source code: getipaddrs.zip
Now and then this question is asked on various .NET newsgroups on how to list all the IP addresses machine is using. .NET Framework Class Library contains System.Net namespace that provides a simple programming interface for many of the protocols used on networks today. This namespace contains Dns class that provides simple domain name resolution functionality. The Dns class is static class that retrieves information about specific host from the Internet Domain Name System (DNS). The host information from the DNS query is returned in an instance of the IPHostEntry class. If the specified host has more than one entry in the DNS database, IPHostEntry contains multiple IP addresses and aliases.
The following code snippet shows how to enumerate all the IP addresses associated with machine using Visual Basic.NET.
Imports System
Imports System.Net
Module Module1
Sub Main()
Dim strMachineName As String
'Get the Host Name
strMachineName = Dns.GetHostName()
Console.WriteLine("Host Name: " + strMachineName)
'Get the Host by Name
Dim ipHost As IPHostEntry
ipHost = Dns.GetHostByName(strMachineName)
'You can also query the DNS database for info on a
'website like www.microsoft.com
'In that case replace the above line as:
'ipHost = Dns.GetHostByName("www.microsoft.com")
'Get the list of addresses associated with the host in an array
Dim ipAddr() As IPAddress = ipHost.AddressList
Dim count As Integer
'Enumerate the IP Addresses
For count = 0 To ipAddr.GetUpperBound(0)
Console.Write("IP Addresses {0}: {1} ", count, _
ipAddr(count).ToString)
Next
End Sub
End Module