C# Ping Ip 网络是否畅通实现
最近项目中需要实现 类 cmd 命令ping 的操作。查看当前Ip是否畅通。代码如下:
第一步 引用:System.Net
代码如下:
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; }