Run the CMD command and the cmd command

Source: Internet
Author: User

Run the CMD command and the cmd command

# Region execute the cmd command
/// <Summary>
/// Execute the cmd command
/// </Summary>
/// <Param name = "commandText"> </param>
/// <Returns> </returns>
Private string ExeCommand (string commandText)
{
Process p = new Process (); // create and instantiate a class for the operation Process: Process
P. StartInfo. FileName = "cmd.exe"; // you can specify the application to be started.
P. StartInfo. UseShellExecute = false; // you can specify whether to use an operating system shell to start the process.
P. StartInfo. RedirectStandardInput = true; // indicates whether the application reads data from the StandardInput stream.
P. StartInfo. RedirectStandardOutput = true; // write application input to the StandardOutput stream.
P. StartInfo. RedirectStandardError = true; // write the error output of the application to the StandarError stream.
P. StartInfo. CreateNoWindow = true; // whether to start the process in the new window
String strOutput = null;
Try
{
P. Start ();
P. StandardInput. WriteLine (commandText); // write the CMD command into the StandardInput stream.
P. StandardInput. WriteLine ("exit"); // write the exit command to the StandardInput stream.
StrOutput = p. StandardOutput. ReadToEnd (); // read all characters of all output streams
P. WaitForExit (); // wait indefinitely until the process exits.
P. Close (); // release the process and Close the process.
}
Catch (Exception e)
{
StrOutput = e. Message;
}
Return strOutput;

}
# Endregion

Usage:

 

This is the path of an executable file (including the name of the executable file) or the command line Statement of cmd.

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.