请问,如何使用C#获取本地网关的IP和MAC地址呢?
请问,如何使用C#获取本地网关的IP和MAC地址呢?
我只会获取本机的IP和MAC地址,两者原理相同吗?
------解决方案--------------------http://bbs.caoyuan.net/viewtopic.php?=&p=45125
------解决方案--------------------http://topic.csdn.net/t/20050320/18/3866172.html
这个可以参考下..
刚刚不好意思
------解决方案--------------------获取本机的所有IP,子网掩码(subnet mask)和默认网关
添加对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]);
}
}
------解决方案--------------------IP地址可以这样
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());
}