usingSystem;usingSystem.Diagnostics;namespaceBusiness {/// <summary> ///a summary description of the Command. /// </summary> Public classCommand {PrivateProcess proc =NULL; /// <summary> ///Construction Method/// </summary> PublicCommand () {proc=NewProcess ();} /// <summary> ///executing a CMD statement/// </summary> /// <param name= "cmd" >the cmd command to execute</param> Public voidRuncmd (stringcmd) {proc. Startinfo.createnowindow=true; Proc. Startinfo.filename="Cmd.exe"; Proc. Startinfo.useshellexecute=false; Proc. Startinfo.redirectstandarderror=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 path plus name (. exe file)</param> /// <param name= "cmd" >the command to execute</param> Public voidRunprogram (stringProgramName,stringcmd) {System.Diagnosties.Process P=NewSystem.Diagnosties.Process (); p.StartInfo.FileName="Cmd.exe";//the name of the program to executeP.startinfo.useshellexecute=false; P.startinfo.redirectstanderinput=true;//may accept input from the calling programp.startinfo.redirectstanderoutput=true;//get output information from the calling programp.startinfo.createnowindow=true;//do not show program windowP.start ();//Start the program//to send input information to the CMD window:P.standerinput.writeline ("Shutdown-r T Ten");//Restart in 10 seconds (C # can not do well)//gets the output information for the cmd window:stringSoutput =P.standardoutput.readtoend (); With the following code, you can operate the cmd without knowing it. In summary, the process class is a very useful class, and it is very convenient to use third-party programs to extend the functionality of C #. if(CMD. Length! =0) {Proc. Standardinput.writeline (CMD); } proc. Close (); } /// <summary> ///Open Software/// </summary> /// <param name= "ProgramName" >software path plus name (. exe file)</param> Public voidRunprogram (stringprogramname) { This. Runprogram (ProgramName,""); } } }
When called
New Command (); cmd. Runcmd ("dir"
Getting output information should be noted:
ReadtoEnd () Easy to get stuck:
string outstr = Proc. Standardoutput.readtoend ();
More inclined to use ReadLine ():
[CSharp] View plaincopyprint? string tmptstr = Proc. Standardoutput.readline (); string "" ; while "" ) { + = outstr ; = Proc. Standardoutput.readline (); }
When invoking a third-party EXE, you can use the following:
/// <summary> ///Executive ThermSpy.exe/// </summary> Private voidCmdthermspy () {Process P=NewProcess (); 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 (); }
Methods for implicitly running CMD command-line windows in C #