C # call the cmd command

Source: Internet
Author: User

usingSystem.Diagnostics; Public classCmdhelper {Private Static stringCmdpath =@"C:\Windows\System32\cmd.exe"; /// <summary>        ///Execute cmd command///For multiple commands, use the Batch command connector:/// <! [Cdata[        ///&: Execute two commands at a time///|: The output from the previous command as input to the next command///&&: When the command before && is successful, the command after && is executed/// || : when | | The previous command failed before execution | | After the command]]>///others please Baidu/// </summary>        /// <param name= "cmd" ></param>        /// <param name= "Output" ></param>         Public Static voidRuncmd (stringCmd out stringoutput) {cmd= cmd. Trim (). TrimEnd ('&') +"&exit";//Note: The exit command is executed regardless of whether the command succeeds or not, and is in suspended animation when the ReadToEnd () method is called .            using(Process p =NewProcess ()) {p.StartInfo.FileName=Cmdpath; P.startinfo.useshellexecute=false;//whether to start with the operating system shellP.startinfo.redirectstandardinput =true;//accept input from the calling programP.startinfo.redirectstandardoutput =true;//get output information from the calling programP.startinfo.redirectstandarderror =true;//REDIRECT standard error OutputP.startinfo.createnowindow =true;//do not show program windowP.start ();//Start the program//writing commands to the cmd windowp.standardinput.writeline (CMD); P.standardinput.autoflush=true; //gets the output information of the cmd windowOutput =P.standardoutput.readtoend (); p.WaitForExit ();//wait for the program to finish executing the exit processP.close (); }}} Use Example 1: Display ipconfig informationstringcmd =@"Ipconfig/all"; Example 2: Open the VS2010 command promptstringcmd =@"C:&CD C:\Program Files (x86) \microsoft Visual Studio 10.0\vc&vcvarsall.bat"; Example 3: Use the Sn.exe tool to generate a key pair and displaystringcmd =@"C:&CD C:\Program Files (x86) \microsoft Visual Studio 10.0\vc&vcvarsall.bat&sn-k D:\LICBASE.SNK&SN -P d:\LicBase.snk d:\licbasepubkey.snk&sn-tp d:\LicBasePubKey.snk"; CallstringOutput =""; Cmdhelper.runcmd (cmd, outoutput); MessageBox.Show (output);

C # call the cmd command

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.