//获取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]); } }
这个可以参考下..刚刚不好意思
添加对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]);
}
}
System.Net.IPHostEntry myHost = new System.Net.IPHostEntry();
myHost = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
for(int i=0; i<myHost.AddressList.Length;i++)
{
Console.WriteLine(myHost.AddressList[i].ToString());
}