C# Ping

In this article I want to show you how to “ping” an IP Address by using C#.

First of all what we should know is what packages are required to accomplish this task, we must use System and System.Net.NetworkInformation packages.

We need an instance an Ping class object to send the ping request through Send() method, then we can manage the ping reply with an object of PingReply class, we can extract useful information from this object (Address, RoundtripTime and Status in this example).

If you have any doubt or want to thank for this article I will gladly read your messages, thank you.

using System;
using System.Net.NetworkInformation;

namespace PingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send("81.2.199.57");
            Console.WriteLine("Address: {0}", pingReply.Address);
            Console.WriteLine("Time in miliseconds: {0}", pingReply.RoundtripTime);
            Console.WriteLine("Status: {0}", pingReply.Status);
            Console.ReadLine();
        }
    }
}

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.