PingTool.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.NetworkInformation;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Y.Utils.NetUtils.NetInfoUtils
  12. {
  13. public class PingTool
  14. {
  15. public static bool CanPing(string ip)
  16. {
  17. return CanPing(ip, 120);
  18. }
  19. public static bool CanPing(string ip, int timeout)
  20. {
  21. try
  22. {
  23. Ping pingSender = new Ping();
  24. PingReply reply = pingSender.Send(ip, timeout);//第一个参数为ip地址,第二个参数为ping的时间
  25. if (reply.Status == IPStatus.Success)
  26. {
  27. //ping的通
  28. return true;
  29. }
  30. else
  31. {
  32. //ping不通
  33. return false;
  34. }
  35. }
  36. catch { }
  37. //异常
  38. return false;
  39. }
  40. }
  41. }