How to get the IP address of the server on which my C# application is running on?

前端 未结 26 2277
天命终不由人
天命终不由人 2020-11-22 06:01

I am running a server, and I want to display my own IP address.

What is the syntax for getting the computer\'s own (if possible, external) IP address?

Someon

26条回答
  •  没有蜡笔的小新
    2020-11-22 06:40

    If you want to avoid using DNS:

    List ipList = new List();
    foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (var address in netInterface.GetIPProperties().UnicastAddresses)
        {
            if (address.Address.AddressFamily == AddressFamily.InterNetwork)
            {
                Console.WriteLine("found IP " + address.Address.ToString());
                ipList.Add(address.Address);
            }
        }
    }
    

提交回复
热议问题