Program | execution
The Process class in C # can easily invoke external programs, so we can call the Cmd.exe program
Add the parameter "/C" + the command to execute to execute a DOS command
(/C indicates that the cmd.exe/k parameter is closed after the command specified by the execution parameter is not closed cmd.exe)
1 private string Runcmd (String command)
2 {
3//In fact, a process type that inspires a unique process
4 Process P = new process ();
5
6//process has a startinfo, this is processstartinfo, including some of the attributes and methods, and we use several of his attributes:
7
8 p.StartInfo.FileName = "cmd.exe"; Set the program name
9 p.startinfo.arguments = "/C" + command; To set the program to perform the parameters
P.startinfo.useshellexecute = false; Shutting down the use of the shell
One p.startinfo.redirectstandardinput = true; REDIRECT Standard input
P.startinfo.redirectstandardoutput = true; REDIRECT The standard output
P.startinfo.redirectstandarderror = true; REDIRECT Error output
P.startinfo.createnowindow = true; Setup does not display windows
15
P.start (); Activate
17
//p.standardinput.writeline (command); You can also enter commands to execute in this way
//p.standardinput.writeline ("Exit"); But remember to add exit or the next line of code
20
return P.standardoutput.readtoend (); From the output stream to get the results of the command.
22
23}