C # Execute DOS command (CMD command)

Source: Internet
Author: User

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)

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.