54

I've got some code here that works great on IPv4 machines, but on our build server (an IPv6) it fails. In a nutshell:

IPHostEntry ipHostEntry = Dns.GetHostEntry(string.Empty);

The documentation for GetHostEntry says that passing in string.Empty will get you the IPv4 address of the localhost. This is what I want. The problem is that it's returning the string "::1:" on our IPv6 machine, which I believe is the IPv6 address.

Pinging the machine from any other IPv4 machine gives a good IPv4 address... and doing a "ping -4 machinename" from itself gives the correct IPv4 address.... but pinging it regularly from itself gives "::1:".

How can I get the IPv4 for this machine, from itself?

8 Answers 8

67

Have you looked at all the addresses in the return, discard the ones of family InterNetworkV6 and retain only the IPv4 ones?

5
  • My call is only returning ipv6, no ipv4 entries. If I do Resolve-DnsName -Type A in PowerShell I get the IPv4 back (default type it returns in this case is AAAA).
    – kamranicus
    Sep 3, 2015 at 20:54
  • @subkamran please ask that as a separate question, not as a comment Sep 4, 2015 at 8:09
  • It's due to Direct Access on my corporate PC. Disregard.
    – kamranicus
    Oct 1, 2015 at 22:46
  • Why can't we use IPv6? Nov 12, 2018 at 21:39
  • @StormMuller because the OP asks "Get IPv4 addresses..." Nov 13, 2018 at 12:29
52

To find all local IPv4 addresses:

IPAddress[] ipv4Addresses = Array.FindAll(
    Dns.GetHostEntry(string.Empty).AddressList,
    a => a.AddressFamily == AddressFamily.InterNetwork);

or use Array.Find or Array.FindLast if you just want one.

0
12
IPHostEntry ipHostInfo = Dns.GetHostEntry(serverName);
IPAddress ipAddress = ipHostInfo.AddressList
    .FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
1
  • Unfortunately is the VirtualBox-address before computer ip address on my machine. I can't select the real ip address because both have the "InterNetwork" flag.
    – Georg
    Nov 21, 2018 at 8:02
5
    public Form1()
    {
        InitializeComponent();

        string myHost = System.Net.Dns.GetHostName();
        string myIP = null;

        for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
        {
            if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
            {
                myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
            }
        }
    }

Declare myIP and myHost in public Variable and use in any function of the form.

2
    public static string GetIPAddress(string hostname)
    {
        IPHostEntry host;
        host = Dns.GetHostEntry(hostname);

        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                //System.Diagnostics.Debug.WriteLine("LocalIPadress: " + ip);
                return ip.ToString();
            }
        }
        return string.Empty;
    }
-1

To find all valid address list this is the code I have used

public static IEnumerable<string> GetAddresses()
{
      var host = Dns.GetHostEntry(Dns.GetHostName());
      return (from ip in host.AddressList where ip.AddressFamily == AddressFamily.lo select ip.ToString()).ToList();
}
-1

You can get all IPv4 address from the DNS using this code:

IPs[] ipv4Addresses = Array.FindAll(
           Dns.GetHostEntry(string.Empty).AddressList,
           address => address.AddressFamily == AddressFamily.InterNetwork);
-5

IPv6

lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()


IPv4

lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(1).ToString()

2
  • 2
    System.Net.Dns.GetHostName is a method, not a property.
    – RenniePet
    Jul 12, 2015 at 12:15
  • can get 2 of ipv6 , 0 not equal ipv4, can be another set of ipv6
    – Neo Yap
    Apr 11, 2020 at 15:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.