tags:

views:

553

answers:

1

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?

+2  A: 

Disclaimer- I don't have IP 6 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()

BTW this may blow up, so don't treat it as production code (First may yield null)!

RichardOD
where does the a come from?
Jonathan
Jonathan- I didn't see the VB tags, so posted my answer in C#. I've now (hopefully) translated it into VB.NET.
RichardOD
thanks, I was thinking thats not VB :)
Jonathan