Get All IP Addresses on Machine

前端 未结 6 615
北恋
北恋 2020-12-01 17:55

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,

6条回答
  •  有刺的猬
    2020-12-01 18:39

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

提交回复
热议问题