Public static void main (string [] ARGs)
{
// Console. writeline ("Hello world! ");
Console. writeline (execute ("netstat-n-B", 0 ));
Console. readkey (true );
}
Public static string execute (string doscommand, int outtime)
{
String output = "";
If (doscommand! = NULL & doscommand! = "")
{
Process = new process (); // create a process object
Processstartinfo startinfo = new processstartinfo (); // a set of values used when creating a process, as shown in the following attributes:
Startinfo. filename = "cmd.exe"; // set the command program to be executed
// The method for hiding the CMD window is as follows:
Startinfo. Arguments = "/C" + doscommand; // set the parameter. To enter the characters in the command program, "/C" indicates that the command exits immediately after execution.
Startinfo. useshellexecute = false; // do not start with the system shell program
Startinfo. redirectstandardinput = false; // The input is not redirected.
Startinfo. redirectstandardoutput = true; // The redirected output is not displayed on the DOS console by default.
Startinfo. createnowindow = true; // do not create a window
Process. startinfo = startinfo;
Try
{
If (process. Start () // start the process
{
If (outtime = 0)
{Process. waitforexit ();}
Else
{Process. waitforexit (outtime );}
Output = process. standardoutput. readtoend (); // read the output of the process.
}
}
Catch
{
}
Finally
{
If (process! = NULL)
{Process. Close ();}
}
}
Return output;
}