Method 1: Call the ping command of CMD.
Private Static string strip Ping (string strip)
{
PROCESS p = new process (); p. startinfo. filename = "cmd.exe"; // set the program name
P. startinfo. useshellexecute = false; // close shell usage
P. startinfo. redirectstandardinput = true; // redirect standard input
P. startinfo. redirectstandardoutput = true; // redirect standard output
P. startinfo. redirectstandarderror = true; // redirect error output
P. startinfo. createnowindow = true; // you can specify not to display a window.
String pingrst; p. Start (); p. standardinput. writeline ("ping" + strip );
P. standardinput. writeline ("exit ");
String strrst = P. standardoutput. readtoend ();
If (strrst. indexof ("(0% loss )")! =-1)
{
Pingrst = "connection ";
}
Else if (strrst. indexof ("destination host unreachable .")! =-1)
{
Pingrst = "unable to reach the target host ";
}
Else if (strrst. indexof ("request timed out .")! =-1)
{
Pingrst = "timeout ";
}
Else if (strrst. indexof ("unknown host ")! =-1)
{
Pingrst = "unable to Resolve Host ";
}
Else
{
Pingrst = strrst;
}
P. Close ();
Return pingrst;
}
Method 2: Use the ping class in C #
Private void displayreply (pingreply reply) // display the result
{
Ping p1 = new Ping (); // only demo, no error handling
Pingreply reply = p1.send ("fill in IP Address ");
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 ));
Response. Write (sbuilder. tostring ());
}
Else if (reply. Status = ipstatus. Timeout)
{
Response. Write ("timeout ");
} Else {
Response. Write ("failed ");
}
}