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

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

    using System.Net;
    
    string host = Dns.GetHostName();
    IPHostEntry ip = Dns.GetHostEntry(host);
    Console.WriteLine(ip.AddressList[0].ToString());
    

    Just tested this on my machine and it works.

提交回复
热议问题