if (NetworkInterface.GetIsNetworkAvailable())
{
String ipv4Address = "";
NetworkInterface[] nic = NetworkInterface.GetAllNetworkInterfaces();
for (int i = 0; i < nic.Length; i++)
{
if (NetworkInterfaceType.Ethernet == nic[i].NetworkInterfaceType)
{
//获取物理地址
PhysicalAddress phyAddress = nic[i].GetPhysicalAddress();
//获取IP地址属性
IPInterfaceProperties inInter = nic[i].GetIPProperties();
//获取单播地址
UnicastIPAddressInformationCollection unIpAddress = inInter.UnicastAddresses;
//获取多个单播地址
IEnumerator<UnicastIPAddressInformation> uipAddInfoEnum = unIpAddress.GetEnumerator();
while (uipAddInfoEnum.MoveNext())
{
//判断是否为IPV4地址
if (AddressFamily.InterNetwork == uipAddInfoEnum.Current.Address.AddressFamily)
{
ipv4Address = uipAddInfoEnum.Current.Address.ToString();
}
}
}
}
if ("" == ipv4Address)
{
MessageBox.Show("Get local ipv4 address failed!");
}
}
else
{
MessageBox.Show("Network is not available!");
}
获取本机ipv4网卡地址
最新推荐文章于 2023-09-13 19:41:39 发布