Trying to get local IP address in VB 2010 - Can't get System.Net.Dns.GetHostEntry to work like System.Net.Dns.Resolve

After 2 hours of trying to do the simplest thing (finding the ip address of the local machine) and searching and searching but getting nowhere fast, I decided to ask my question here.

Apparently, System.Net.Dns.Resolve() is obsolete and I should use System.Net.Dns.GetHostEntry() instead.

I read that GetHostEntry() can simply be used instead of Resolve() as the paramaters and return types are the same but they're not.

Dim myHostName As String = System.Net.Dns.GetHostName()

Dim myIPAddress As String = System.Net.Dns.Resolve(myHostName).AddressList(0).ToString() returns "" which is what I want.


Dim myIPAddress As String = System.Net.Dns.GetHostEntry(myHostName).AddressList(0).ToString() returns "fe80::112:6c34:8cc7:492d%10" which is Greek to me.

I gave up and left it using Resolve() but was hoping that someone could show me how to do it with a non-obsolete function.


Environment: VB.Net / Visual Studio 2010 / Windows Forms Application on Win 7 x64
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The IPHostEntry will populate IPv6 address information as well so you can have multiple address entries in the AddressList. You can filter to return only IPv4 by checking the AddressFamily specifically Sockets.InterNetwork here is more information about AddressFamily enumeration
Dim entry As IPHostEntry = Dns.GetHostEntry(String.Empty)
        For Each ip As IPAddress In entry.AddressList
            ' Only display ipv4 address
            If ip.AddressFamily = Sockets.AddressFamily.InterNetwork Then
            End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ou81aswellAuthor Commented:
Thanks. I didn't realize the list contained different address families.

I'm confused about there being 3 addresses in my address list. Which IP address is the one that Internet Explorer uses to browse the web?
The IPAddress returned that looks like is your computers network address usually obtained automatically from your DHCP server/router. May I ask what your real goal is because GetHostEntry() might not be what your looking for depending on what your trying to accomplish in your application. Could you elaborate?
ou81aswellAuthor Commented:
I have a client app and a server app that use sockets to talk to each other across my LAN.

They talk together well.

I want to put the local IP address in the title bar of both apps
ou81aswellAuthor Commented:
Thanks. Still not sure whic IP address to use. It appears that it's the first one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.