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

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

    If you are running in intranet you'll be able to get local machine IP address and if not you'll get external ip address with this: Web:

    //this will bring the IP for the current machine on browser
    System.Web.HttpContext.Current.Request.UserHostAddress
    

    Desktop:

    //This one will bring all local IPs for the desired namespace
    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
    

提交回复
热议问题