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

lixinyu306 lixinyu306 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
自己写的代码如下 :运行后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;
}
}

}

}
}
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
lixinyu306
其他开发语言 3,425 成员 15,624 内容