用net2.0 实现ping功能
/*
*
* User: 张健滢
* Date: 2005-12-18
* Time: 22:20
*
*
*/
using System;
using System.Security.Cryptography;
using System.Net.NetworkInformation;//新增加的名称空间
using System.Text;
namespace Demo
{
class MainClass
{
Ping _ping=new Ping();
public static void Main(string[] args)
{
MainClass C=new MainClass();
C.DisPlay("www.csdn.net");
C.DisPlay("jsj.hnhgzy.com");
}
private void DisPlay(string Source){
try{
//超时找不到抛出错误
PingReply traget=_ping.Send(Source);
//判断连接状态
if (traget.Status == IPStatus.Success){
StringBuilder output=new StringBuilder();
output.Append("地址: "+traget.Address.ToString());
output.AppendLine();//换行,这个也是net2.0新增加的方法哦
output.Append("连接状态: "+traget.Status.ToString());
output.AppendLine();
output.Append("响应时间: "+traget.RoundtripTime.ToString("N"));
Console.WriteLine(output );
}
}catch(System.Net.NetworkInformation.PingException e)
{
Console.WriteLine("System.Net.NetworkInformation.PingException" );
Console.WriteLine(e.Message);
}
}
}
}