C#获取本机名及IP地址

时间:2022-08-27 16:04:13
using System;
using System.Collections.Generic;
using System.Text;
using System.Net; //需要引用.Net命名空间

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//获得主机名
string HostName = Dns.GetHostName();
Console.WriteLine("主机名是:" + HostName);
//遍历地址列表,如果电脑有多网卡只能这样遍历显示
IPAddress[] iplist = Dns.GetHostAddresses(HostName);
for (int i = 0; i < iplist.Length; i++)
{
Console.WriteLine("IP地址:" + iplist[i]);
}
Console.ReadLine();
}
}
}