Methods for implicitly running CMD command-line windows in C #

Source: Internet
Author: User

 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 #

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.