In C #, the command line window of CMD is implicitly run,

Source: Internet
Author: User

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 ();}

 

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.