易丰科技

标题: c# code snippet 判断是否在线 [打印本页]

作者: Calvin    时间: 2011-10-3 19:54
标题: c# code snippet 判断是否在线
  1. //判断是否在线
  2.         public static bool IsOnline()
  3.         {
  4.             if (!NetworkInterface.GetIsNetworkAvailable())
  5.                 return false;
  6.             NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  7.             foreach (NetworkInterface networkInterface in networkInterfaces)
  8.             {
  9.                 if (networkInterface.OperationalStatus != OperationalStatus.Up)
  10.                     continue;

  11.                 IPInterfaceProperties properties = networkInterface.GetIPProperties();
  12.                 UnicastIPAddressInformationCollection infos = properties.UnicastAddresses;
  13.                 foreach (UnicastIPAddressInformation info in infos)
  14.                 {
  15.                     if (System.Net.IPAddress.IsLoopback(info.Address))
  16.                         continue;
  17.                     string sInfo = info.Address.ToString();
  18.                     if (sInfo == System.Net.IPAddress.IPv6None.ToString())
  19.                         continue;
  20.                     if (sInfo == System.Net.IPAddress.None.ToString())
  21.                         continue;
  22.                     return true;


  23.                 }
  24.             }
  25.             return false;
  26.         }
复制代码






欢迎光临 易丰科技 (http://forum.yftek.com/) Powered by Discuz! X3