Abejali Technology

The Way To Success
Subscribe

Archive for the ‘VB.Net’

How To Get My IPv4 or IPv6 Address

November 10, 2009 By: abejali Category: VB.Net 1 Comment →

This is my simple function to get local IPv4 and IPv6 Address ..
Feel free to use it at your own ..

Public Class Form1

    Private Function GetAllMyAddress() As Array
        'System.Net.Dns.GetHostName
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
        Return h.AddressList.ToArray()
    End Function

    Private Function GetMyAddress(ByVal addressFamily As Net.Sockets.AddressFamily) As String
        'System.Net.Dns.GetHostName
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
        For Each address In h.AddressList
            If address.AddressFamily = addressFamily Then
                Return address.ToString
            End If
        Next
        Return "0.0.0.0"
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(String.Format("My IPv4 Address Is : {0}", _
                GetMyAddress(Net.Sockets.AddressFamily.InterNetwork)))
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox(String.Format("My IPv6 Address Is : {0}", _
                GetMyAddress(Net.Sockets.AddressFamily.InterNetworkV6)))
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each item In GetAllMyAddress()
            MsgBox(item.ToString)
        Next
    End Sub

End Class

Download This Project  Here