最近項目中須要實現 類 cmd 命令ping 的操做。查看當前Ip是否暢通。代碼以下:orm
第一步 引用:System.Netip
代碼以下:cmd
protected bool Ping(string ip) { Ping p = new System.Net.NetworkInformation.Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = "Test Data!"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 1000; PingReply reply = p.Send(ip, timeout, buffer, options); if (reply.Status == IPStatus.Success) return true; else return false; }