private void GetIPv4()
{
string hostName = Dns.GetHostName();//本机名
//System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址
System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6
foreach (IPAddress ip in addressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
listBox1.Items.Add(ip.ToString());
}
}
c# getipv4
最新推荐文章于 2022-07-31 10:16:05 发布