c# Ping with Simple Pinger Example


using System;
using System.Threading;
using System.Windows.Forms;
using System.Net.NetworkInformation;
 
 
namespace ContextHelper
{
    static class Pinger
    {
        public static void Ping(string url_or_IP)
        {
            using (Ping ping = new Ping())
            {
 
                try
                {
                    Console.Write("    {0}...", url_or_IP);
                    PingReply reply = ping.Send(url_or_IP, 100);
                    if (reply.Status == IPStatus.Success)
                    {
                        Console.WriteLine("Success - IP Address:{0}", reply.Address, reply.RoundtripTime);
                    }
                    else
                    {
                        Console.WriteLine(reply.Status);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error ({0})",
                        ex.InnerException.Message);
                }
 
            }
        }
    }
}
Advertisements

About bpostaci
Sr. Support Escalation Engineer in Microsoft.

3 Responses to c# Ping with Simple Pinger Example

  1. ComputerTips says:

    Thanks for this great code mate!

    You solved the problem in no time.

    For other readers: This ping code is working perfectly fine for me.

  2. naseriax says:

    simple and awesome. it works great, thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: