System.Net.NetworkInformation空间提供对网络流量数据、网络地址信息和本地计算机的地址更改通知的访问。该命名空间还包含实现 Ping 实用工具的类。可以使用Ping和相关的类检查是否可通过网络连接到计算机。在查MSDN时无意间翻到这了,发现挺好玩的,MSDN还给了具体例子,暂且记下来,说不定以后就用到了.
1 public static void ShowNetwork() 2 { 3 NetworkInterface[] networkInterface = 4 NetworkInterface.GetAllNetworkInterfaces(); 5 foreach (NetworkInterface adapter in networkInterface) 6 { 7 Console.WriteLine("描述------------:" + adapter.Description); 8 Console.WriteLine("网络适配器的名称:" + adapter.Name); 9 Console.WriteLine("接口类型--------:" + adapter.NetworkInterfaceType); 10 Console.WriteLine("状态------------:" + adapter.OperationalStatus); 11 PhysicalAddress address = adapter.GetPhysicalAddress(); 12 byte[] bytes = address.GetAddressBytes(); 13 Console.Write("MAC-------------:"); 14 for (int i = 0; i < bytes.Length; i++) 15 { 16 Console.Write("{0}", bytes[i].ToString("X2")); 17 if (i != bytes.Length - 1) 18 { 19 Console.Write("-"); 20 } 21 } 22 Console.WriteLine(); 23 Console.WriteLine("=======分割线========="); 24 Console.WriteLine(); 25 } 26 }