/// <Summary>
/// Execute the doscommand and return the doscommand output
/// </Summary>
/// <Param name = "doscommand"> doscommand </param>
/// If it is set to 0, it will be infinitely waiting </param>
/// <Returns> return the doscommand output </returns>
Public static string execute (string command, int seconds)
{
String output = ""; // output string
If (command! = NULL &&! Command. Equals (""))
{
Process = new process (); // create a process object
Processstartinfo startinfo = new processstartinfo ();
Startinfo. filename = "cmd.exe"; // set the command to be executed
Startinfo. Arguments = "/C" + command; // "/C" indicates to exit immediately after the command is executed.
Startinfo. useshellexecute = false; // do not start with the system shell program
Startinfo. redirectstandardinput = false; // The input is not redirected.
Startinfo. redirectstandardoutput = true; // redirect output
Startinfo. createnowindow = true; // do not create a window
Process. startinfo = startinfo;
Try
{
If (process. Start () // start the process
{
If (seconds = 0)
{
Process. waitforexit (); // There is no limit to wait until the process ends.
}
Else
{
Process. waitforexit (seconds); // wait for the process to end. The waiting time is specified in milliseconds.
}
Output = process. standardoutput. readtoend (); // read the output of the process.
}
}
Catch
{
}
Finally
{
If (process! = NULL)
Process. Close ();
}
}
Return output;
}