Use the C # external ping command to obtain the network connection status
In the past, when I was playing Windows 98's, several computers were connected, and the network connection test was normal. A common command was called ping.exe. It feels quite practical.
Now the dig command is used to detect the network, and it is helpful for beginners of. net.
First, we use the process class to create independent processes and import system. diagnostics,
Using system. diagnostics;
Starts an independent process.
PROCESS p = new process ();
The process class has a startinfo attribute, which is the processstartinfo class, including some attributes and methods,
Below we use several of its attributes:
SetProgramName
P. startinfo. filename = "cmd.exe ";
Disable shell usage
P. startinfo. useshellexecute = false;
Redirect standard input
P. startinfo. redirectstandardinput = true;
Redirection standard output
P. startinfo. redirectstandardoutput = true;
Redirect error output
P. startinfo. redirectstandarderror = true;
Set do not display window
P. startinfo. createnowindow = true;
Setting the above attributes is a key step.
Now that all the settings are complete, start the process,
P. Start ();
Enter the command to be executed. Here the ping command is used,
P. standardinput. writeline ("Ping-N 1 192.192.132.229 ");
P. standardinput. writeline ("exit ");
Obtain the command execution result from the output stream,
String strrst = P. standardoutput. readtoend ();
Test on the local machine to obtain the following results:
"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 \ npinging 192.192.132.231 with 32 bytes of data: \ r \ n \ r \ nreply from 192.192.132.231: bytes = 32 time <10 ms TTL = 128 \ r \ n \ r \ nping statistics for 192.192.132.231: \ r \ n Packets: Sent = 1, inclued = 1, lost = 0 (0% loss), \ r \ napproximate round trip times in Milli-seconds: \ r \ n minimum = 0 ms, maximum = 0 ms, average = 0ms \ r \ n \ r \ Nd: \ himuraz \ csharpproject \ ZZ \ consoletest \ bin \ debug> exit \ r \ n"
With the output result, there is something to say. You can analyze the strrst string to know the network connection.
The following is a program. Of course, the execution result of the ping.exe program is incomplete, and you can modify it further.
CompleteCodeAs follows:
Using system;
Using system. diagnostics;
Namespace ZZ
{
Class zzconsole
{
[Stathread]
Static void main (string [] ARGs)
{
String IP = "192.192.132.229 ";
String strrst = Ping (IP );
Console. writeline (strrst );
Console. Readline ();
}
Private Static string strip Ping (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 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;
}
}
}
To sum up, this is to illustrate a problem. We can use the following methods to execute the ping command as long as it is a command line program or dos internal command, and obtain the corresponding results, the execution process of these programs is not displayed. If you need to call an external program, you can embed it into it for use.