本文共 726 字,大约阅读时间需要 2 分钟。
// Net2.0新增类库,记得引用
using System.Net.NetworkInformation;
/// <summary>
/// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败 /// </summary> /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param> /// <returns></returns> private bool PingIP(string DoNameOrIP) { try { Ping objPingSender = new Ping(); PingOptions objPinOptions = new PingOptions(); objPinOptions.DontFragment = true; string data = ""; byte[] buffer = Encoding.UTF8.GetBytes(data); int intTimeout = 120; PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions); string strInfo = objPinReply.Status.ToString(); if (strInfo == "Success") { return true; } else { return false; } } catch (Exception) { return false; } }转载地址:http://nhumf.baihongyu.com/