C # Use the ping command

Source: Internet
Author: User

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 ");

}

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.