在.net framework2.0中实现ping方法
.net framework2.0提供了System.Net.NetWorkInformation命名空间,我们可以用此命名空间下的ping类,来很简单的实现ping的方法。
以下是实现的代码:
以下是实现的代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
namespace PingDotNET
{
class Program
{
static void Main(string[] args)
{
// 显示详细情况
Console.WriteLine("Ping in .NET Framework 2.0 ");
Console.WriteLine("用 .NET Framework 2.0编写 ");
// 让用户输入主机名,得到ping的ip地址
if (args.Length == 0)
{
Console.WriteLine("Usage: PingDotNET ");
return;
}
// 初始化ping类
Ping refPing = new Ping();
PingReply refPingResult = null;
// 调用ping的send方法
Console.WriteLine("Pinging {0} ",args[0]);
refPingResult = refPing.Send(args[0]);
Console.WriteLine("Ping Status: {0}",refPingResult.Status.ToString());
Console.WriteLine("RoundTrip Time [millisec]: {0}",refPingResult.RoundTripTime);
if (refPingResult.Options != null)
Console.WriteLine("TTL: {0}",refPingResult.Options.Ttl.ToString());
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
namespace PingDotNET
{
class Program
{
static void Main(string[] args)
{
// 显示详细情况
Console.WriteLine("Ping in .NET Framework 2.0 ");
Console.WriteLine("用 .NET Framework 2.0编写 ");
// 让用户输入主机名,得到ping的ip地址
if (args.Length == 0)
{
Console.WriteLine("Usage: PingDotNET ");
return;
}
// 初始化ping类
Ping refPing = new Ping();
PingReply refPingResult = null;
// 调用ping的send方法
Console.WriteLine("Pinging {0} ",args[0]);
refPingResult = refPing.Send(args[0]);
Console.WriteLine("Ping Status: {0}",refPingResult.Status.ToString());
Console.WriteLine("RoundTrip Time [millisec]: {0}",refPingResult.RoundTripTime);
if (refPingResult.Options != null)
Console.WriteLine("TTL: {0}",refPingResult.Options.Ttl.ToString());
}
}
}