Use the C # to call the external ping command to get the network connection
Author: Zheng Zuo 2004-03-20
When I played Windows 98, several computers were connected, and if I need to test whether the network connection is normal, one of the frequent uses is ping.exe. It feels quite practical.
Now .NET provides us with powerful features to call external tools and outputs the execution result by redirect input, and uses an example to illustrate the detection of the network. Net beginners Helped.
First, we use the Process class to create an independent process, import system.diagnostics,
Using system.diagnostics;
An example of an Process class, start a standalone process
PROCESS P = new process ();
The Process class has a StartInfo property, this is the ProcessStartInfo class, including some properties and methods,
Below we use his properties:
Set the program name
p.Startinfo.fileName = "cmd.exe";
Close the use of shell
P.Startinfo.uShellexecute = false;
Redirect standard input
P.Startinfo.redirectStandardInput = true;
Redirect standard output
P.Startinfo.RedirectStandardOrdoutput = true;
Redirect error output
P.StartInfo.RedirectStandarderror = true;
Set up not display window
P.StartInfo.createnowindow = true;
The setting of several attributes above is a relatively critical step.
Since all set it, start the process,
p.Start ();
Enter the command to be executed, here is ping,
P.standardinput.writeline ("ping -n 1 192.192.132.229");
P.standardinput.writeline ("exit");
Get the command execution result from the output stream,
String strrst = p.standardoutput.readtoend ();
The following results were obtained in this machine:
"Microsoft Windows 2000 [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/nping 192.192.132.231 with 32 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 packets: Sent = 1, Received = 1, Lost = 0 (0% Loss), / R / R / NAPPROXIMATE ROUND TRIP 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 "has output results, then what is good, analyze the Strrst string, you can know the connection of the network.
Below is a complete program, of course, the result of the PING.EXE program is not full, the reader can further modify
The full 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";
Strir 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.uShellexecute = false;
P.Startinfo.redirectStandardInput = true;
P.Startinfo.RedirectStandardOrdoutput = 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 = "Connect";
Else IF (STRST.INDEXOF ("Destination Host Unreachable.")! = - 1)
Pingrst = "The host that cannot be reached";
ELSE IF ("Request Timed Out.")! = - 1) pingrst = "Timeout";
Else IF (STRST.INDEXOF ("Unknown Host")! = - 1)
Pingrst = "Unable to resolve the host";
Else
pingrst = strrs;
p.Close ();
Return pingrst;
}
}
}
Summary, here is to illustrate a problem, not only the ping command, as long as it is a command line program or the DOS internal command, we can use the above way to perform it, and get the corresponding result, and these programs will not be Displayed, if you need to call an external program, you can embed it.