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);
                }
 
            }
        }
    }
}
Advertisement

About bpostaci
Escalation Engineer in Microsoft.

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: