新手。请问:怎么获取以太网IPv4的地址和DNS?

lixinyu306 2017-11-21 03:10:00
自己写的代码如下 :运行后DNS服务器出来的总是IPV6的格式,请问各位大佬应该怎么改?

private void button1_Click(object sender, EventArgs e)
{
NetworkInterface[] accard = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface adapter in accard)
{
bool pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet);//判断是否连接以太网

if (pd1)
{
IPInterfaceProperties ip = adapter.GetIPProperties();//IP配置信息

UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
foreach (UnicastIPAddressInformation ipadd in ipCollection)
{
if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
IPAddress.Text = ipadd.Address.ToString();

}



if (ip.GatewayAddresses.Count > 0)
{
Gateway.Text = ip.GatewayAddresses[0].Address.ToString();//获取默认网关
}

int DNSCount = ip.DnsAddresses.Count;

if (DNSCount > 0)
{
try
{
DNSserver.Text = ip.DnsAddresses[0].ToString();//获取首选DNS地址和备选DNS地址
secDNS.Text = ip.DnsAddresses[0].ToString();
}
catch (Exception ex)
{
throw ex;
}
}

}

}
}
...全文
339 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧