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.