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

前端 未结 26 1990
天命终不由人
天命终不由人 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:47

    Cleaner and an all in one solution :D

    //This returns the first IP4 address or null
    return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
    

提交回复
热议问题