Results 1 to 7 of 7

Thread: [RESOLVED] Local IP Finder not functioning properly in win 7

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Resolved [RESOLVED] Local IP Finder not functioning properly in win 7

    Hello!

    Here is a code that shows my local (LAN) IP in a textbox.

    'To get local IP address
    Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)

    Me.TextBox1.Text = h.AddressList.GetValue(0).ToString



    the code is working, but one warning is generated i.e.

    Warning 'Public Shared Function GetHostByName(hostName As String) As System.Net.IPHostEntry' is obsolete: 'GetHostByName is obsoleted for this type, please use GetHostEntry instead.


    Then I replaced GetHostByName with GetHostEntry:


    'To get local IP address
    Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

    Me.TextBox1.Text = h.AddressList.GetValue(0).ToString

    It works fine with win XP, but in win7 it gives me some weird text...probably mac addres or something.

    Question is: How do make it work for win7?

    Thanks in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Local IP Finder not functioning properly in win 7

    I think you'll find that what you are getting is an IPv6 address. Instead of just using GetValue(0), actually look at the IPAddress objects in the AddressList to see which one(s) is IPv4 and use that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Local IP Finder not functioning properly in win 7

    Following jmcilhinney's suggestion, try this code:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetwork) ' Show IPV4
            TextBox2.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetworkV6)' Show IPV6
        End Sub
    
        Private Function GetHostIP(ByVal af As System.Net.Sockets.AddressFamily) As String
    
            Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
            Dim strRet As String = ""
    
            For Each ip As System.Net.IPAddress In host.AddressList
                If ip.AddressFamily = af Then
                    strRet = ip.ToString
                    Exit For
                End If
            Next
    
            Return strRet
    
        End Function
    
    End Class



  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Local IP Finder not functioning properly in win 7

    Quote Originally Posted by 4x2y View Post
    Following jmcilhinney's suggestion, try this code:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetwork) ' Show IPV4
            TextBox2.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetworkV6)' Show IPV6
        End Sub
    
        Private Function GetHostIP(ByVal af As System.Net.Sockets.AddressFamily) As String
    
            Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
            Dim strRet As String = ""
    
            For Each ip As System.Net.IPAddress In host.AddressList
                If ip.AddressFamily = af Then
                    strRet = ip.ToString
                    Exit For
                End If
            Next
    
            Return strRet
    
        End Function
    
    End Class
    Not to say that there's anything wrong with that code, because the steps are all there laid out, but here's the concise version:
    vb.net Code:
    1. Dim address = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(Function(ia) ia.AddressFamily = af)
    2.  
    3. Return If(address Is Nothing, CStr(Nothing), address.ToString())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Local IP Finder not functioning properly in win 7

    @jmcilhinney

    I like your way of coding although it is difficult to understand from first look



  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Local IP Finder not functioning properly in win 7

    When I write code like that for real I tend to use line breaks to improve clarity:
    vb.net Code:
    1. Dim address = Dns.GetHostEntry(Dns.GetHostName()).
    2.                   AddressList.
    3.                   FirstOrDefault(Function(ia) ia.AddressFamily = af)
    4.  
    5. Return If(address Is Nothing,
    6.           CStr(Nothing),
    7.           address.ToString())
    It can be confusing to read if you're not familiar with each element though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Re: Local IP Finder not functioning properly in win 7

    Used 4x2y code: works fine on both platforms (win XP and win 7).
    Thank you all for helping.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width