Search code examples
c#.netwcfnetworkingip

Get All IP Addresses on Machine


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?


Solution

  • 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)."