Ping a Remote Server Using C#

In my last post, I shared some code in VBScript to PING a remote server to check for availability before attempting to use a resource on that remote server. This wouldn’t be my blog if I didn’t also share this code in C# as well. IMO, this sort of thing is a far better suited for a .NET Extension if for the SalesLogix Windows client.

To perform a PING using C#, the code is far more straight forward than the VBScript version. No need for shelling the command out and reading back the StdOut. This version reads much nicer:

using System;
using System.Management;

namespace FX.Network.Utility
{
    public class NetworkDiscovery
    {
        public NetworkDiscovery() { }

        public static bool Ping(string Target)
        {
            SelectQuery query = new SelectQuery("Win32_PingStatus", string.Format("Address='{0}'", Target));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

            foreach (ManagementObject result in searcher.Get())
            {
                return (result["StatusCode"] != null && (0 == (UInt32)result["StatusCode"]));
            }
            return false;
        }
    }
}

Enjoy!

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

1 Comment

  1. Hi ryan!I wonder if you can help me doing a code for C# on how to ping sever and will display its TTL and latency same thing when you ping a server using CMD prompt, also to display whether the handshake was successful or not.I will really appreciate if you can do it for me.thanks in advanced.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!