VB.Net | Get IP address of a computer against hostname | Fun projects

Credits: http://stackoverflow.com/questions/19713868/returning-ipv6-instead-of-ipv4-in-vb

How to?

Open Visual Studio IDE (We are using VS 2012 Express and the .Net framework is 4.5 by default, hence if few things do not work at your setups, please cross check the specs we specified) and create a Windows form application. We’ll be mostly doing our samples using forms, a better mechanism to test stuffs against console applications (Minimum from our experiences)

drag a button to the form

drag a listbox to the form

& do not change any property for any of the above items

right click on the form and select view code, once the code page opens
Ctrl+A (Select all and delete)

copy and paste the below code block (replacing the original content of the code page)

[code language=”vb” gutter=”false” wraplines=”true”]
Imports System.Net

Public Class Form1
Public Function getip(ByVal lst As ListBox) As ListBox
Dim hostEntry = Dns.GetHostEntry("hostname")
Dim addressList As New List(Of IPAddress)

For Each address In hostEntry.AddressList
If address.AddressFamily = Sockets.AddressFamily.InterNetwork Then
lst.Items.Add(address)
End If
Next
Return lst

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
getip(ListBox1)
End Sub
End Class
[/code]

Now replace the “hostname” with some actual hostnames and execute the application by pressing F5. Click the button over the form you are done! Please note, this sample is restricted to fetch IPV4 addresses.