In the Windows environment, the command-line program for Cmd.exe, is a 32-bit command-line program, Microsoft Windows system based on the Windows command interpreter, similar to Microsoft's DOS operating system. Enter some commands, Cmd.exe can be executed, such as input shutdown-s will shut down in 30 seconds. In short, it is very useful. Open method: Start-All Programs-attachments or start-find-Input: Cmd/cmd.exe enter. It can also execute a bat file.
The following is an introduction to executing commands using C # programs called CMD:
Code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Diagnostics;7 8 namespaceCmddemo9 {Ten class Program One { A Static voidMain (string[] args) - { -Console.WriteLine ("Please enter the command you want to execute:"); the stringStrinput =console.readline (); -Process p =NewProcess (); - //set up the application to start -p.StartInfo.FileName ="Cmd.exe"; + //whether to start with the operating system shell -P.startinfo.useshellexecute =false; + //accept input from the calling program AP.startinfo.redirectstandardinput =true; at //Output Information -P.startinfo.redirectstandardoutput =true; - //Output Error -P.startinfo.redirectstandarderror =true; - //do not show program window -P.startinfo.createnowindow =true; in //Start the program - P.start (); to + //send input information to the CMD window -P.standardinput.writeline (strinput+"&exit"); the *P.standardinput.autoflush=true; $ Panax Notoginseng //Get output Information - stringStrouput =p.standardoutput.readtoend (); the //wait for the program to finish executing the exit process + p.waitforexit (); A p.close (); the + Console.WriteLine (strouput); - $ Console.readkey (); $ } - } -}
Operating effect:
Application: Generating a client call file for a WCF service using the C # program call cmd command
Design interface:
The code is as follows:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.Diagnostics; One A namespaceExecutecmd - { - Public Partial classFrmmain:form the { - Publicfrmmain () - { - InitializeComponent (); + } - + Private voidBtn_create_click (Objectsender, EventArgs e) A { at Try - { - //Create a process -Process p =NewProcess (); -p.StartInfo.FileName ="Cmd.exe"; -P.startinfo.useshellexecute =false;//whether to start with the operating system shell inP.startinfo.redirectstandardinput =true;//accept input from the calling program -P.startinfo.redirectstandardoutput =true;//get output information from the calling program toP.startinfo.redirectstandarderror =true;//REDIRECT standard error Output +P.startinfo.createnowindow =true;//do not show program window -P.start ();//Start the program the * stringStrcmd ="\""+@"C:\Program Files (x86) \microsoft Sdks\windows\v8.0a\bin\netfx 4.0 Tools\svcutil.exe"+"\" "+ This. Txt_url. Text.tostring (). Trim () $+"/R:"+"\""+@"C:\Program Files (x86) \reference Assemblies\microsoft\framework\. Netframework\v4.0\system.data.dll"+"\""+"/synconly";Panax Notoginseng //send input information to the CMD window -P.standardinput.writeline (Strcmd +"&exit"); the +P.standardinput.autoflush =true; A the //gets the output information of the cmd window + stringOutput =p.standardoutput.readtoend (); - //wait for the program to finish executing the exit process $ p.waitforexit (); $ p.close (); - - the MessageBox.Show (output); - Console.WriteLine (output);Wuyi } the Catch(Exception ex) - { WuMessageBox.Show (ex. Message +"\ r \ n Tracking;"+Ex. StackTrace); - } About } $ } -}
Click the Create button and the CS file will be generated under the Bin\Debug directory.
C # program calls CMD to execute command