c#获取真实网卡ip地址_asp.net取物理网卡ip地址

发布时间:2019-09-18编辑:脚本学堂
一例c#获取真实网卡Ip地址的代码,asp.net取物理网卡的真实IP地址,有需要的朋友参考下。

当你用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();
}