使用 IPHostEntry.AddressList 属性获取 IP 地址 -
IPHostEntry myIP = Dns.GetHostEntry(hostName);
IPAddress[] address = myIP.AddressList;
登录后复制
尝试以下代码来显示 IP 地址 -
示例
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();
}
}
登录后复制
以上就是如何使用C#显示机器的IP地址?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!