源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace CSharpTest
{
class Program
{
static void Main(string[] args)
{
Console.Write(new Program().GetHostInfo());
Console.ReadLine();
}
//获取本地IPd等信息
protected string GetHostInfo()
{
StringBuilder hostInfo = new StringBuilder("");
IPAddress[] ipHost = Dns.GetHostAddresses(Dns.GetHostName());
hostInfo.Append("本机名:");
hostInfo.Append(Dns.GetHostName());
hostInfo.Append("\r\n");
hostInfo.Append("IP 地址:");
hostInfo.Append("\r\n");
foreach (IPAddress address in ipHost)
{
hostInfo.Append(address.ToString());
hostInfo.Append("\r\n");
}
return hostInfo.ToString();
}
}
}
控制台输出结果:
本机名:meteor-PC
IP 地址:
fe80::a1a2:949c:1a51:5e6c%11
2002:b46f:20fa::b46f:20fa
169.254.94.108
180.111.32.250
注:前面两个IP地址是IPv6地址(测试环境Window7+.Net Framework 3.5)
如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。