Use C # to invoke an external ping command to obtain network connectivity

Source: Internet
Author: User


Use C # to invoke an external ping command to obtain network connectivity

In the past, when playing Windows 98, several computers connected, need to test the network connection is normal, often use a command is Ping.exe. It feels pretty practical.

Now. NET provides us with a powerful function to invoke external tools, and through redirecting input, output to obtain execution results, the following is an example to illustrate the call Ping.exe command to implement network detection, I hope to help. NET Beginners.

First, we use the process class to create separate processes, import system.diagnostics,

Using System.Diagnostics;

Instance a process class that initiates a standalone procedure

Process P = new process ();

The process class has a StartInfo property, which is the ProcessStartInfo class, including some properties and methods,

Here's a few of his attributes:

Set Program Name

p.StartInfo.FileName = "cmd.exe";

Turn off the use of the shell

P.startinfo.useshellexecute = false;

REDIRECT Standard input

P.startinfo.redirectstandardinput = true;

REDIRECT Standard output

P.startinfo.redirectstandardoutput = true;

REDIRECT Error output

P.startinfo.redirectstandarderror = true;

Settings do not display windows

P.startinfo.createnowindow = true;

The setting of the above attributes is a more critical step.

Now that it's all set, start the process.

P.start ();

Enter the command you want to execute, and here's the ping.

P.standardinput.writeline ("Ping-n 1 192.192.132.229");

P.standardinput.writeline ("Exit");

Gets the result of the command execution from the output stream,

String strrst = P.standardoutput.readtoend ();

The following results were obtained in the native test:

"Microsoft Windows C [Version 5.00.2195]/r/n (C) Copyright 1985-2000 Microsoft corp./r/n/r/nd://himuraz//csharpproject// Zz//consoletest//bin//debug>ping-n 1 192.192.132.231/r/n/r/r/npinging 192.192.132.231 with bytes of data:/r/r/n/ R/r/nreply from 192.192.132.231:bytes=32 time<10ms ttl=128/r/r/n/r/r/nping statistics for 192.192.132.231:/r/r/n Pa Ckets:sent = 1, Received = 1, Lost = 0 (0% loss),/r/r/napproximate round trips times in milli-seconds:/r/r/n Minimum = 0ms, Maximum = 0ms, Average = 0ms/r/r/n/r/nd://himuraz//csharpproject//zz//consoletest//bin//debug>exit/r/n "

With the output, there is nothing to say, analysis of the Strrst string to know the network connectivity.

The following is a complete program, of course, the results of the implementation of the Ping.exe program is not complete, the reader can further modify

The complete code is as follows:

Using System;

Using System.Diagnostics;

Namespace ZZ

{

Class Zzconsole

{

[STAThread]

static void Main (string[] args)

{

string ip = "192.192.132.229";

String strrst = cmdping (IP);

Console.WriteLine (Strrst);

Console.ReadLine ();

}

private static string cmdping (String strIp)

{

Process P = new process ();

p.StartInfo.FileName = "cmd.exe";

P.startinfo.useshellexecute = false;

P.startinfo.redirectstandardinput = true;

P.startinfo.redirectstandardoutput = true;

P.startinfo.redirectstandarderror = true;

P.startinfo.createnowindow = true;

String Pingrst;

P.start ();

P.standardinput.writeline ("Ping-n 1" +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 destination 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;

}

}

}


To sum up, here is to illustrate a problem, not only is the ping command, as long as it is a command-line program or a DOS internal command, we can execute it in the above way, and get the corresponding results, and the execution of these programs will not be displayed, if you need to call the external program can be embedded in the use of it.
Http://hexun.com/metababy

Related Article

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.