using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.NetworkInformation;

class MyPing
{
    public static void Main(String[] args)
    {
        MyPing myPing = new MyPing();
        String rzText = myPing.ping("tw.yahoo.com");
        Console.WriteLine(rzText);
    }

    public String ping(String ip)
    {
        Ping ping = null;
        PingReply reply = null;

        String rzText = "";
        String errorText = "";

        if (ip.Length != 0)
        {
            int i = 0;
            int received = 0;

            for (i = 0; i <= 3; i++)
            {
                try
                {
                    ping = new Ping();

                    reply = ping.Send(ip);

                    if (reply.Status == IPStatus.Success)
                    {
                        rzText +=
                            "Reply from " + reply.Address.ToString() +
                            ": bytes=" + reply.Buffer.Length +
                            " time=" + reply.RoundtripTime.ToString(NumberFormatInfo.CurrentInfo) + "ms" +
                            " TTL=" + reply.Options.Ttl + "\r\n";

                        received += 1;
                    }
                    else
                        rzText += GetPingReplyStatus(reply.Status) + "\r\n";
                }
                catch (Exception ex)
                {
                    errorText = ex.StackTrace.ToString();
                }
            }

            int loss = 4 - received;

            rzText += "\r\nPing statistics for " + ip + ":" + "\r\n";
            rzText += "Packets: Sent = 4, Received = " + received + ", Lost = " + loss;
        }
        else
        {
            Console.WriteLine("Please enter a host name or IP address.");
        }
        return rzText;
    }

    private string GetPingReplyStatus(IPStatus status)
    {
        switch (status)
        {
            case IPStatus.Success:
                return "Success.";
            case IPStatus.DestinationHostUnreachable:
                return "Destination host unreachable.";
            case IPStatus.DestinationNetworkUnreachable:
                return "Destination network unreachable.";
            case IPStatus.DestinationPortUnreachable:
                return "Destination port unreachable.";
            case IPStatus.DestinationProtocolUnreachable:
                return "Destination protocol unreachable.";
            case IPStatus.PacketTooBig:
                return "Packet too big.";
            case IPStatus.TtlExpired:
                return "TTL expired.";
            case IPStatus.ParameterProblem:
                return "Parameter problem.";
            case IPStatus.SourceQuench:
                return "Source quench.";
            case IPStatus.TimedOut:
                return "Timed out.";
            default:
                return "Ping failed.";
        }
    }
}