dll:// C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0System.dll
方法 1:
/// <summary> /// 是否能 Ping 通指定的主机 /// </summary> /// <param name="ip">ip 地址或主机名或域名</param> /// <returns>true 通,false 不通</returns> private bool Ping(string ip) { bool w_blnReturn = false; try { System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions(); options.DontFragment = true; string data = "Test Data!"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 1000; // Timeout 时间,单位:毫秒 System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) w_blnReturn = true; else w_blnReturn = false; } catch (Exception ex) { //异常日志 msg = String.Format("Ping执行异常,详细:{0}", ex.StackTrace.ToString()); log.Error(msg); } return w_blnReturn; }
方法2: PingReply reply = pinSender.Send(str_ip, timeout, buffer, options);
/// <summary> /// Ping固定IP /// </summary> /// <param name="str_ip">IP地址</param> /// <returns>成功返回1,失败返回0</returns> public int CheckDeviceStatus(string str_ip) { Ping pinSender = new Ping(); PingOptions options = new PingOptions(); string data = ""; byte[] buffer = Encoding.ASCII.GetBytes(data); options.DontFragment = true; int timeout = 1200; PingReply reply = pinSender.Send(str_ip, timeout, buffer, options); if (reply.Status != IPStatus.Success) { return 0; } return 1; }