C# 多网卡IP MAC 获取
网上查了不少代码,大都用两个不相关的代码来获取IP和MAC,
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
{
mac = mac+mo["MacAddress"].ToString()"\r\n";
}
}
操作方法
- 01
mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["IPEnabled"].ToString() == "True") { mac = mac+mo["MacAddress"].ToString()+" ip "+ ((string[])mo["IPAddress"])[0].ToString()+"\r\n"; } } 如过一网卡有多IP 则先将IP数组提出来, string[] tmp_IP=(string[])mo["IPAddress"]; ip1=tmp_IP[0]; ip2=tmp_IP[1]; .......
- 02
........
- 03
...............