-4

I want to ping any ip address and get the return value which shalt let me know whether it was successful or not. Something like that

if(ping.Equals(success)) { ... }
doe
  • 17
  • 4

2 Answers2

1

First result on google for "C# Ping". Examples near the bottom of the page.

But basically boils down to

Ping pingSender = new Ping ();
PingReply reply = pingSender.Send (ip, timeout);
if (reply.Status == IPStatus.Success)
     //Success
else
     //Failure
Ben Abraham
  • 482
  • 5
  • 10
  • Thanks! Why did Jan use var and you didn't? Any advantages? – doe Aug 01 '16 at 20:13
  • 1
    Both are functionally equivalent and will produce the same output code. My way is simply more verbose, but both ways are valid. var means the compiler will interpret the type for you, without needing to specify. Comes in handy when dealing with generics. – Ben Abraham Aug 01 '16 at 20:15
0
var sender = new Ping();
var result = sender.Send("127.0.0.1");
if (Result.Status == IPStatus.Success)
    // OK
else
    // it failed

Please note that you will need to add this using directive: System.Net.NetworkInformation

Jan
  • 108
  • 1
  • 7
  • @downvoter why don't you state any reasons? It matched the op's question. – Jan Aug 01 '16 at 20:05
  • 2
    I'm not the downvoter, but some people do object to people answering low effort questions on principle. An upvoted answer will also prevent the question from being auto-deleted after it's closed. – Matt Burland Aug 01 '16 at 20:14
  • @MattBurland thank you for letting me know. I just wanted to help. – Jan Aug 01 '16 at 20:23