3

I tried to get the IPAddress of my computer using this

        var ipadd = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ipAddress in ipadd.AddressList)
            Console.WriteLine("IP Address: {0}", ipAddress);

I have only one network card in my computer which is connected to the router. It is ipv4 but this line of code gives me 4 IPAddress 3 of them are ipv6 and one is ipv4 which is the valid one. I like to ask why is that so ?

Thanks

Ahmed
  • 636
  • 1
  • 14
  • 31

1 Answers1

4
foreach (var addr in Dns.GetHostEntry(string.Empty).AddressList)
{
    if (addr.AddressFamily == AddressFamily.InterNetwork)
        Console.WriteLine("IPv4 Address: {0}", addr)
}
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • 1
    Thanks it works.. you are using "Dns.GetHostEntry(string.Empty)" and I have seen on other places that they use "Dns.GetHostEntry(Dns.GetHostName());"... It doesn't matter if pass Dns.GetHostName() or not ? – Ahmed Feb 21 '11 at 14:11