如何获取本机的所有IP子网掩码和默认网关?
添加对System.Management的引用
using System.Management;
。。。
//获取IP,子网掩码(subnet mask)和默认网关
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach(ManagementObject nic in nics)
{
if(Convert.ToBoolean(nic["ipEnabled"]) == true)
{
Console.WriteLine((nic["IPAddress"] as String[])[0]);
Console.WriteLine((nic["IPSubnet"] as String[])[0]);
Console.WriteLine((nic["DefaultIPGateway"] as String[])[0]);
}
}
。。。
如果还需要其他信息参考 PlatformSDK:Windows Management Instrumentation
本文地址:http://www.45fan.com/dnjc/72052.html