Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("");
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...