troubleshooting Question

Problem pinging non existent hosts in C#

Avatar of piattnd
piattnd asked on
C#
7 Comments1 Solution595 ViewsLast Modified:
Experts:

Pardon my ignorance on the subject of C#, I'm just beginning. :)

I'm writing an application that has a need to test connectivity to a host provided by the user before performing other actions.  I've attached the code that I use and it works if I provide a valid host name, but if I give an invalid host name, it crashes with the error that the host is not resolvable.  I understand DNS won't resolve the host, but how can I catch that error and have the program react to it rather than crashing?
string Computer = computerName_input.Text;

            Ping ping = new Ping();
            PingReply reply = ping.Send(Computer);

            if (reply.Status == IPStatus.Success)
            {
                MessageBox.Show("Results for " + Computer + ": " + reply.Status.ToString());
            }
            else
            {
                MessageBox.Show("Machine is offline");
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros