|
-
Sep 20th, 2012, 03:09 AM
#1
Thread Starter
Lively Member
[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!
-
Sep 20th, 2012, 04:43 AM
#2
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.
-
Sep 20th, 2012, 05:21 AM
#3
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
-
Sep 20th, 2012, 05:43 AM
#4
Re: Local IP Finder not functioning properly in win 7
 Originally Posted by 4x2y
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:
Dim address = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(Function(ia) ia.AddressFamily = af)
Return If(address Is Nothing, CStr(Nothing), address.ToString())
-
Sep 20th, 2012, 06:46 AM
#5
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
-
Sep 20th, 2012, 08:36 AM
#6
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:
Dim address = Dns.GetHostEntry(Dns.GetHostName()).
AddressList.
FirstOrDefault(Function(ia) ia.AddressFamily = af)
Return If(address Is Nothing,
CStr(Nothing),
address.ToString())
It can be confusing to read if you're not familiar with each element though.
-
Sep 21st, 2012, 03:06 AM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|