First, add the textbox, listbox, And button controls. Enter the domain name or IP address for textbox, and display the result for listbox.
Type
Private void button#click (object sender, EventArgs e)
{
Ping p1 = new Ping (); // only demo, no error handling
PingReply reply = p1.Send (this. textBox1.Text); // blocked displayReply (reply); // display result} private void displayReply (PingReply reply) // display result
{
StringBuilder sbuilder;
If (reply. Status = IPStatus. Success)
{
Sbuilder = new StringBuilder ();
Sbuilder. Append (string. Format ("Address: {0}", reply. Address. ToString ()));
Sbuilder. Append (string. Format ("RoundTrip time: {0}", reply. RoundtripTime ));
Sbuilder. Append (string. Format ("Time to live: {0}", reply. Options. Ttl ));
Sbuilder. Append (string. Format ("Don't fragment: {0}", reply. Options. DontFragment ));
Sbuilder. Append (string. Format ("Buffer size: {0}", reply. Buffer. Length ));
ListBox1.Items. Add (sbuilder. ToString ());
}
}
You can also perform asynchronous processing, modify the button#click, and add the PingCompletedCallBack method.
Private void button#click (object sender, EventArgs e)
{
Ping p1 = new Ping ();
P1.PingCompleted + = new PingCompletedEventHandler (this. PingCompletedCallBack); // set the PingCompleted event handler
P1.SendAsync (this. textBox1.Text, null );
}
Private void PingCompletedCallBack (object sender, PingCompletedEventArgs e)
{
If (e. Cancelled)
{
ListBox1.Items. Add ("Ping Canncel ");
Return;
}
If (e. Error! = Null)
{
ListBox1.Items. Add (e. Error. Message );
Return;
}
StringBuilder sbuilder;
PingReply reply = e. Reply;
If (reply. Status = IPStatus. Success)
{
Sbuilder = new StringBuilder ();
Sbuilder. Append (string. Format ("Address: {0}", reply. Address. ToString ()));
Sbuilder. Append (string. Format ("RoundTrip time: {0}", reply. RoundtripTime ));
Sbuilder. Append (string. Format ("Time to live: {0}", reply. Options. Ttl ));
Sbuilder. Append (string. Format ("Don't fragment: {0}", reply. Options. DontFragment ));
Sbuilder. Append (string. Format ("Buffer size: {0}", reply. Buffer. Length ));
ListBox1.Items. Add (sbuilder. ToString ());
}
}