您的位置:首页 > 编程语言 > C#

C# 获取本机指定类型指定网卡的Ip地址

2016-01-16 16:19 435 查看
1.简单方式 ,适用于单网卡

string ipv4 = "";
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
//Wireless80211 无线网卡     Ethernet 以太网卡
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{

//网卡Mac地址
System.Net.NetworkInformation.PhysicalAddress mac = adapter.GetPhysicalAddress();
//TODO:根据mac 地址 匹配目标网卡

//获取以太网卡网络接口信息
IPInterfaceProperties ip = adapter.GetIPProperties();
//获取单播地址集
UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
foreach (UnicastIPAddressInformation ipadd in ipCollection)
{
if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{
ipv4= ipadd.Address.ToString();//获取ip
}
else if (ipadd.Address.AddressFamily == AddressFamily.InterNetworkV6)
{
//本机IPV6 地址
}
}
}
}


支持网卡类型 网卡mac筛选
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: