C # Call the cmd command and the doscommand

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.