28

How can I get all of the IP addresses attached to the machine that my application (C# NET Console app) is running on? I need to bind a WCF service to the primary IP address, and return a list of the full IP address list.

using System.Net;

string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();

This is what I am using right now to get the Primary IP address, but I can't figure out how to get the rest to return them.

If I bind a WCF service to localhost:8000, will that expose it on the primary?

stivlo
  • 83,644
  • 31
  • 142
  • 199

6 Answers6

66

The DNS variants work across the network, but one DNS entry can have many IP addresses and one IP address can have many DNS entries. More importantly, an address needn't be bound to a DNS entry at all.

For the local machine try this:

foreach (NetworkInterface netInterface in 
    NetworkInterface.GetAllNetworkInterfaces())
{
    Console.WriteLine("Name: " + netInterface.Name);
    Console.WriteLine("Description: " + netInterface.Description);
    Console.WriteLine("Addresses: ");

    IPInterfaceProperties ipProps = netInterface.GetIPProperties();

    foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
    {
        Console.WriteLine(" " + addr.Address.ToString());
    }

    Console.WriteLine("");
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
10

I think this example should help you.

// Get host name
String strHostName = Dns.GetHostName();

// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
    ....
}

Edit:

"There's no such thing as a "primary" IP address.

The routing table determines which outward-facing IP address is used depending on the destination IP address (and by extension, the network interface, which itself can be virtual or physical)."

Alex Hope O'Connor
  • 9,354
  • 22
  • 69
  • 112
  • This will typically return what you want but on occasion if there is a misconfiguration in DNS on the system you can have some issues with Dns.GetHostByName. – kthompson Jul 10 '15 at 16:03
  • This answer actually does not work in my machine. It only returns 127.0.0.1, even though I have an active Wifi connection with a real ip-address. – avl_sweden Sep 18 '15 at 14:24
  • This answer has a bug, it does not return all the local machine IP addresses because an IP address don't need to be bound to a DNS host. Using NetworkInterface.GetAllNetworkInterfaces like in the answer below should be the accepted answer. Don't use this code! – Darxis Apr 14 '21 at 23:00
6

Why not just bind to 0.0.0.0 ?
That way you listen on all ips

Fredrik Leijon
  • 2,792
  • 18
  • 20
1

You should probably bind to 0.0.0.0:8000, that will expose it on all available IP addresses and only bind to a particular IP address if the user/administrator demands so.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
0
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
StefanE
  • 7,578
  • 10
  • 48
  • 75
-2

I think the OP is asking about how to get all addresses on a local NIC, not just those addresses known to DNS. By primary he probably means the main address under "use the following IP address" in the adapter properties, and by "the rest" he probably means those listed in Advanced > (Additional) IP Addesses.

DNS will not necessarily know those.