Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get local IPv4 of computer using VB.net

Tags:

vb.net

ipv4

I'm trying to get the ip address of my local PC, and one one of my other PCs it gets the v4 address fine, but on this one the code:

Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString()

returns what I guess is a IPv6 address:

fe80::9c09:e2e:4736:4c62%11

How do I get the IPv4 address?

like image 445
Jonathan. Avatar asked Oct 29 '09 08:10

Jonathan.


3 Answers

Disclaimer- I don't have IPv6 installed and there is probably a much better way to do this, but what does the following return:

Dns.GetHostEntry(Dns.GetHostName()).AddressList
    .Where(a => !a.IsIPv6LinkLocal && !a.IsIPv6Multicast && !a.IsIPv6SiteLocal)
    .First()
    .ToString();

Edit - didn't notice you were asking in VB, so I've tried translating it to:

Dim s As String = Dns.GetHostEntry(Dns.GetHostName()).AddressList _
    .Where(Function(a As IPAddress) Not a.IsIPv6LinkLocal AndAlso Not a.IsIPv6Multicast AndAlso Not a.IsIPv6SiteLocal) _
    .First() _
    .ToString()

This may blow up, so don't treat it as production code.

like image 193
RichardOD Avatar answered Oct 21 '22 02:10

RichardOD


Here's my solution for getting a routable IPv4 IP without using an external service:

  Function GetLocalIP() As String
    Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

    For Each IPaddress In IPList.AddressList
      'Only return IPv4 routable IPs
      If (IPaddress.AddressFamily = Sockets.AddressFamily.InterNetwork) AndAlso (Not IsPrivateIP(IPaddress.ToString)) Then
        Return IPaddress.ToString
      End If
    Next
    Return ""
  End Function

  Function IsPrivateIP(ByVal CheckIP As String) As Boolean
    Dim Quad1, Quad2 As Integer

    Quad1 = CInt(CheckIP.Substring(0, CheckIP.IndexOf(".")))
    Quad2 = CInt(CheckIP.Substring(CheckIP.IndexOf(".") + 1).Substring(0, CheckIP.IndexOf(".")))
    Select Case Quad1
      Case 10
        Return True
      Case 172
        If Quad2 >= 16 And Quad2 <= 31 Then Return True
      Case 192
        If Quad2 = 168 Then Return True
    End Select
    Return False
  End Function

Note that my code is also verifying that the range is routable (IsPrivateIP). You can remove or modify that part if you are looking for something else.

like image 25
Bryan Avatar answered Oct 21 '22 01:10

Bryan


I used a combined Cmd/Visual Basic code and it worked :

    Dim ResString As String = "result.txt"

    If File.Exists("result.txt") Then
        File.Delete("result.txt")
    End If

    Shell("cmd.exe /c  cd " & Application.StartupPath & " && ipconfig >> " & ResString & "&& exit", AppWinStyle.NormalFocus)

    Dim Ipv4 As String
    Dim Ipv4Found As Boolean = False
    Dim Ipv4Char As Integer = 43
    Dim Ipv4Str As String
    Threading.Thread.Sleep(1500)
    'Wait some seconds to create "result.txt"

    Dim Ipv4Reader As StreamReader
    Ipv4Reader = File.OpenText("result.txt")

    Do Until Ipv4Found = True

        Ipv4Str = Ipv4Reader.ReadLine()
        If Not Ipv4Str = Nothing Then
            If Ipv4Str.Contains("IPv4") Then
                Try
                    Ipv4 = Ipv4Str.Chars(Ipv4Char)
                    Do Until Ipv4Char = 60
                        Ipv4Char = Ipv4Char + 1
                        Ipv4 = Ipv4 & Ipv4Str.Chars(Ipv4Char)
                        'Read results step by step
                    Loop
                Catch ex As Exception
                End Try
                MsgBox("Your IPv4 Address is " & Ipv4)
                Ipv4Found = True
                Ipv4Reader.Close()

            End If
        Else

        End If
    Loop

If your computer language is english you may have some unusual characters in the IPv4 String ( My pc is actually in Italian )

like image 37
Ale865 Avatar answered Oct 21 '22 01:10

Ale865