In C #, the command line window of CMD is implicitly run,
Using System; using System. Diagnostics; namespace Business {/// <summary> /// Command abstract description. /// </Summary> public class Command {private Process proc = null; // <summary> // constructor /// </summary> public Command () {proc = new Process ();} /// <summary> /// execute the CMD statement /// </summary> /// <param name = "cmd"> the CMD command to be executed </param> public void RunCmd (string cmd) {proc. startInfo. createNoWindow = true; proc. startInfo. fileName = "cmd.exe"; proc. startInfo. useShellExecute = false; proc. startInfo. redirect StandardError = true; proc. startInfo. redirectStandardInput = true; proc. startInfo. redirectStandardOutput = true; proc. start (); proc. standardInput. writeLine (cmd); proc. close () ;}/// <summary> /// open the software and execute the command /// </summary> /// <param name = "programName"> Software register name (.exe file) </param> /// <param name = "cmd"> command to be executed </param> public void RunProgram (string programName, string cmd) {System. diagnosties. process p = new System. diagnosties. process (); p. startInfo. fileName = "cmd.exe"; // name of the program to be executed p. startInfo. useShellExecute = false; p. startInfo. redirectStanderInput = true; // the input information from the caller may be accepted. startInfo. redirectStanderOutput = true; // get the output information by the caller p. startInfo. createNoWindow = true; // do not display the program window p. start (); // Start the program // send the input information to the CMD window: p. standerInput. writeLine ("shutdown-r t 10"); // restart after 10 seconds (C # may be difficult to do) // obtain the output information of the CMD window: string sOutput = p. standard Output. ReadToEnd (); with the following code, you can operate CMD without knowing it. In short, the Process class is a very useful class, it is very convenient to use third-party programs to expand the C # function. If (cmd. Length! = 0) {proc. standardInput. writeLine (cmd);} proc. close () ;}/// <summary> /// open the software /// </summary> /// <param name = "programName"> Software register name (.exe file) </param> public void RunProgram (string programName) {this. runProgram (programName ,"");}}}
Call time
Command cmd = new Command(); cmd.RunCmd("dir");
Note the following when getting output information:
ReadtoEnd () is easy to get stuck:
string outStr = proc.StandardOutput.ReadtoEnd();
More inclined to use ReadLine ():
[csharp] view plaincopyprint?string tmptStr = proc.StandardOutput.ReadLine(); string outStr = ""; while (tmptStr != "") { outStr += outStr; tmptStr = proc.StandardOutput.ReadLine(); }
When calling third-party exe, you can use the following:
/// <Summary> /// execute thermspy.exe // </summary> private void implements thermspy () {Process p = new Process (); p. startInfo. workingDirectory = Environment. currentDirectory; p. startInfo. fileName = "ThermSpyPremium.exe"; p. startInfo. arguments = "-header: gpu-datetime-log: NV_clock.log"; p. startInfo. createNoWindow = true; p. startInfo. useShellExecute = false; p. startInfo. redirectStandardOutput = true; p. start ();}