/// <Summary>
/// Execute the doscommand and return the doscommand output
/// </Summary>
/// <Param name = "doscommand"> doscommand </param>
/// <Param name = "milliseconds"> the waiting time (unit: milliseconds) for command execution. If it is set to 0, the waiting time is unlimited. </param>
/// <Returns> returns the output. If an exception occurs, an empty string is returned. </returns>
Public static string executedos (string doscommand, int milliseconds)
{
String output = ""; // output string
If (doscommand! = NULL & doscommand! = "")
{
Process = new process (); // create a process object
Processstartinfo startinfo = new processstartinfo ();
Startinfo. filename = "cmd.exe"; // set the command to be executed
Startinfo. Arguments = "/C" + doscommand; // set the parameter. "/C" indicates that the system exits immediately after the command is executed.
Startinfo. useshellexecute = false; // The system shell is not used. Program Start
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 (milliseconds = 0)
Process. waitforexit (); // There is no limit to wait until the process ends.
Else
Process. waitforexit (milliseconds); // wait until the process ends. The wait time is specified in milliseconds.
Output = process. standardoutput. readtoend (); // read the output of the process.
}
}
Catch
{
}
Finally
{
If (process! = NULL)
Process. Close ();
}
}
Return output;
}