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

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

    Nope, that is pretty much the best way to do it. As a machine could have several IP addresses you need to iterate the collection of them to find the proper one.

    Edit: The only thing I would change would be to change this:

    if (ip.AddressFamily.ToString() == "InterNetwork")
    

    to this:

    if (ip.AddressFamily == AddressFamily.InterNetwork)
    

    There is no need to ToString an enumeration for comparison.

提交回复
热议问题