Test Connection To host / Is host reachable / PING functionality C#

Hi,

We can check the particular machine is get ping or not from using C# code.
Also, we can get the Round-triptime of packet.
Using Ping and PingReply class, we can do this.

The code is as below

Ping x = new Ping();
PingReply reply = x.Send(IPAddress.Parse("192.168.1.1")); //enter ip of the machine
if (reply.Status == IPStatus.Success) // here we check for the reply status if it is success it means the host is reachable
{
status.Text = "Available. And Round Trip Time of the packet is:"+reply.RoundtripTime.ToString();
}
else //if host is not reachable.
status.Text = "Not available";

Just 3-4 lines will solve the issue.

That’s it.
Enjoy Coding.. 🙂