ping in C#

This is what I recommend for checking if a remote host is pingable.

This code is from a diagnostics tool which runs in production for many years.

public static bool Ping(string host)
{
    for (int pass = 0; pass < 3; ++pass)
    {
        try
        {
            Ping pingSender = new Ping();
            PingReply reply = pingSender.Send(host);
            if (reply.Status == IPStatus.Success)
                return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    return false;
}

Leave a Reply