Public Class Testcmd
{
Public Void Operationcmd ()
{
String Dosstring1 = " Command 1 " ;
String Dosstring2 = " Command 2 " ;
Execute (dosstring1 );
Execute (dosstring2 );
}
Public String Execute ( String Doscommand)
{
Return Execute (doscommand, 6 * 1000 );
}
/// <Summary>
/// Run the doscommand to return the doscommand output.
/// </Summary>
/// <Param name = "doscommand"> Doscommand </Param>
/// <Param name = "milliseconds"> The time (in milliseconds) for waiting for command execution. If the value is set to 0, the system waits infinitely. </Param>
/// <Returns> Returns the doscommand output. </Returns>
Public Static String Execute ( String Doscommand, Int Milliseconds)
{
String Output ="" ; // Output string
If (Doscommand! = Null & Doscommand! = "" )
{
Process = New Process (); // Create process object
Processstartinfo startinfo = New Processstartinfo ();
Startinfo. filename = " Cmd.exe " ; // Set the command to be executed
Startinfo. Arguments = " /C " + Doscommand; // Set parameters. "/C" indicates that the system exits immediately after the command is executed.
Startinfo. useshellexecute = False ;// No System ShellProgramStart
Startinfo. redirectstandardinput = False ; // Do not redirect Input
Startinfo. redirectstandardoutput = True ; // Redirection output
Startinfo. createnowindow = True ; // Do not create a window
Process. startinfo = startinfo;
Try
{
If (Process. Start ()) // Start Process
{
If (Milliseconds = 0 )
Process. waitforexit (); // Wait for the process to end infinitely.
Else
Process. waitforexit (milliseconds ); // Wait for the process to end. The wait time is specified in milliseconds.
Output = process. standardoutput. readtoend (); // Read process output
}
}
Catch
{
}
Finally
{
If (Process! = Null )
Process. Close ();
}
}
Return Output;
}
}