当你用ipconfig命令查看网址IP地址信息时,会显示一些非真实网卡的IP信息,如何过滤呢?可以参考如下代码。
例子,c#获取真实网卡IP地址。
using System.Management;
using System.Management.Instrumentation;
ManagementClass mc_nac = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc_nac = mc_nac.GetInstances();
foreach (ManagementObject mo in moc_nac)
{
string mServiceName = mo["ServiceName"] as string;
//过滤非真实网卡
if (!(bool)mo["IPEnabled"])
{ continue; }
if (mServiceName.ToLower().Contains("vmnetadapter")
|| mServiceName.ToLower().Contains("ppoe")
|| mServiceName.ToLower().Contains("bthpan")
|| mServiceName.ToLower().Contains("tapvpn")
|| mServiceName.ToLower().Contains("ndisip")
|| mServiceName.ToLower().Contains("sinforvnic"))
{ continue; }
bool mDHCPEnabled = (bool)mo["IPEnabled"];
string mCaption = mo["Caption"] as string;
string mMACAddress = mo["MACAddress"] as string;
string[] mIPAddress = mo["IPAddress"] as string[];
string[] mIPSubnet = mo["IPSubnet"] as string[];
string[] mDefaultIPGateway = mo["DefaultIPGateway"] as string[];
string[] mDNSServerSearchOrder = mo["DNSServerSearchOrder"] as string[];
if (mIPAddress != null)
{
foreach (string ip in mIPAddress)
{
if (ip != "0.0.0.0")
{
IpAddress.item.Add(ip);
}
}
}
mo.Dispose();
}