在C#中,PingReply類主要用于接收Ping操作的結果。可以通過以下步驟來使用PingReply類:
首先,創建一個Ping對象:
using System.Net.NetworkInformation;
Ping ping = new Ping();
使用Ping對象的Send方法發送Ping請求,并獲取PingReply對象:
string ipAddress = "127.0.0.1"; // 要ping的IP地址
int timeout = 1000; // 超時時間(單位:毫秒)
PingReply reply = ping.Send(ipAddress, timeout);
可以通過PingReply對象的各種屬性來獲取Ping操作的結果信息。例如:
Console.WriteLine("IP地址: " + reply.Address);
Console.WriteLine("是否可達: " + reply.Status);
Console.WriteLine("往返時間: " + reply.RoundtripTime);
完整的示例代碼如下所示:
using System;
using System.Net.NetworkInformation;
namespace PingExample
{
class Program
{
static void Main(string[] args)
{
Ping ping = new Ping();
string ipAddress = "127.0.0.1";
int timeout = 1000;
PingReply reply = ping.Send(ipAddress, timeout);
Console.WriteLine("IP地址: " + reply.Address);
Console.WriteLine("是否可達: " + reply.Status);
Console.WriteLine("往返時間: " + reply.RoundtripTime);
Console.ReadLine();
}
}
}