使用函数 dns.gethostname()就可以了的,新建一个解决方案,命名为 getDnsName
具体的代码如下(C#):
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace getDnsName
{
class Program
{
static void Main(string[] args)
{
string name = Dns.GetHostName();
Console.WriteLine("主机名字:{0}", name);
IPHostEntry me = Dns.GetHostByName(name);
foreach (IPAddress ip in me.AddressList)
{
Console.WriteLine("IP地址:{0}", ip.ToString());
Console.Read();
}
}
}
}
测试成功!