Private void button3_Click (object sender, EventArgs e)
{
If (dq1ip. Text. Trim ()! = "")
{
Execute ("mstsc/v:" + dq1ip. Text );
}
}
Public string Execute (string dosCommand)
{
Return Execute (dosCommand, 10 );
}
/// <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 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;
}
From Bychentufeiyang's column