Home >Backend Development >C#.Net Tutorial >How to display the IP address of a machine using C#?

How to display the IP address of a machine using C#?

王林
王林forward
2023-09-06 22:25:02837browse

How to display the IP address of a machine using C#?

Get the IP address using the IPHostEntry.AddressList property -

IPHostEntry myIP = Dns.GetHostEntry(hostName);
IPAddress[] address = myIP.AddressList;

Try the following code to display the IP address-

Example

using System;
using System.Net;

class Program {
   static void Main() {
      String hostName = string.Empty;
      hostName = Dns.GetHostName();
      Console.WriteLine("Hostname: "+hostName);
      IPHostEntry myIP = Dns.GetHostEntry(hostName);

      IPAddress[] address = myIP.AddressList;
      for (int i = 0; i < address.Length; i++) {
         Console.WriteLine("IP Address {1} : ",address[i].ToString());
      }
      Console.ReadLine();
   }
}

The above is the detailed content of How to display the IP address of a machine using C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete