Saturday, August 14, 2010

Use Ping in C#

Many probably know the ping command from the command prompt. With this data packages can be send to different addresses, to computers in a network or servers in the internet, for testing purposes.
If they packages are successfully sent and is the destination address reachable, an appropriate response is given.
With the programming language C# such a request can also easily be send. First we have to include the class needed therefor via using:
using System.Net.NetworkInformation;

With the function Send() of the class Ping a ping can be send. The overloading with 1 parameter used here just sets the target address (127.0.0.1 identifies the own computer).
Optinally there can be used more parameters, the 2nd parameter for example sets the time interval, which will be waited for a response.

Ping Sender = new Ping();
PingReply Result = Sender.Send("127.0.0.1");
if (Result.Status == IPStatus.Success)
    MessageBox.Show("Success);
else
    MessageBox.Show("Failure");

No comments:

Post a Comment