C# 检查网络通畅

public static bool PingServer(string ipOrHostName)
         {
             try
             {
                 Ping ping = new Ping();

                 PingOptions pingOptions = new PingOptions();

                 pingOptions.DontFragment = true;

                 string data = "";

                 byte[] buffer = Encoding.UTF8.GetBytes(data);

                 int timeout = 120;

                 PingReply reply = ping.Send(ipOrHostName, timeout, buffer, pingOptions);

                 string result = reply.Status.ToString();

                 if (result == "Success")
                 {
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             catch (Exception)
             {
                 return false;
             }
         }

君子性非异也,善假于物也!
原文地址:https://www.cnblogs.com/zhaoCat/p/5818999.html