In C # programs, sometimes using the call cmd command to complete some functions, so on the Internet to find the following methods, to achieve the
C # executes the DOS command and returns the result.
//doscommand DOS command statements public String Execute (String doscommand) { return Execute (Doscommand, ten); } ///<summary> Execute DOS command, Returns the output of a DOS command ///</summary> //<param name= "doscommand" >dos command </param> //< param name= "milliseconds" > Wait for Command execution time (in milliseconds), //If set to 0, infinite wait </ param> //<returns> Returns the output of the DOS command </returns> public static string Execute (String command, int seconds) { String output = ""; Output string if (command! = null &&!command. Equals ("")) { process Process = new process ();//Create a Progress object ProcessStartInfo startinfo = new ProcessStartInfo (); Startinfo.filename = "cmd.exe";//SET command to execute startinfo.arguments = "/C" + command;//"/C" to exit immediately after executing the command Startinfo.useshellexecute = false;//does not start with the system shell Startinfo.redirectstandardinput = false;//do not redirect input Startinfo.redirectstandardoutput = true; REDIRECT Output Startinfo.createnowindow = true;//do not create window process. StartInfo = startinfo; try { if ( Process. Start ())//BEGIN process { if ( seconds = = 0) { process. WaitForExit ();//Here indefinitely wait for process to end } else { process. WaitForExit (seconds); Wait for the process to end, waiting time for the specified millisecond } output = process. Standardoutput.readtoend ();//output of the read process } } catch { } &nbSp finally { if ( Process! = NULL) process. Close (); } } return output; }
C # Execute DOS command (CMD command)