tags:

views:

501

answers:

3

I need my application to ping an address I'll specify later on and just simply copy the Average Ping Time to a .Text of a Label.

Any help?

EDIT:

I found the solution in case anyone is interested:

Ping pingClass = new Ping();        
PingReply pingReply = pingClass.Send("logon.chronic-domination.com");
label4.Text = (pingReply.RoundtripTime.ToString() + "ms");
+11  A: 

Give a look the NetworkInformation.Ping class.

An example:

Usage:

PingTimeAverage("stackoverflow.com", 4);

Implementation:

public static double PingTimeAverage(string host, int echoNum)
{
    long totalTime = 0;
    int timeout = 120;
    Ping pingSender = new Ping ();

    for (int i = 0; i < echoNum; i++)
    { 
        PingReply reply = pingSender.Send (host, timeout);
        if (reply.Status == IPStatus.Success)
        {
            totalTime += reply.RoundtripTime;
        }
    }
    return totalTime / echoNum;
}
CMS
Thank you for your answer. Worked wonderfully. :D
Sergio Tapia
You're welcome!
CMS
PingTimeAverage ("i.dont.exist.com", 1000) == 0
Anton Tykhyy
@Anton, if the host cannot be resolved, the Ping.Send method will throw a PingException... http://is.gd/2hLjo
CMS
A: 

Hello Friend

Is it possible in C# to implement the command line version of Ping which will print 4 lines with the time taken, rather than calculating the total time?

I have tried, here and there but unable to find any implementation of ping in comparison to its command line version.

Thanks

Looking for a favorable reply

Everest
A: 

Just as a sidenote to this. There is allready a project on sourceforge doing about that what you want. This has also included an implementation of ICMP (RFC 792)

Sourceforge Project

schoetbi