1. 添加程序集
2. 引入命名空间
代码如下 | 复制代码 |
using System.Management; |
3. 方法
代码如下 | 复制代码 |
ManagementClass mc =newManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection nics = mc.GetInstances(); foreach(ManagementObject nicinnics) { if(Convert.ToBoolean(nic["ipEnabled"]) ==true) { stringmac = nic["MacAddress"].ToString();//Mac地址 stringip = (nic["IPAddress"]asString[])[0];//IP地址 stringipsubnet = (nic["IPSubnet"]asString[])[0];//子网掩码 stringipgateway = (nic["DefaultIPGateway"]asString[])[0];//默认网关 } } |