C # issue the doscommand and obtain the returned value

Source: Internet
Author: User

Private void mybutton2_click (Object sender, eventargs e) <br/>{< br/> // execute ("Net user zero 0571m.com/Add", 0 ); <br/> string output = execute ("Ver" + "/R/N", 0); <br/> console. writeline (output); <br/>}< br/> Public static string execute (string doscommand, int outtime) <br/>{< br/> string output = ""; <br/> If (doscommand! = NULL & doscommand! = "") <Br/>{< br/> Process = new process (); // create a process object <br/> processstartinfo startinfo = new processstartinfo (); // a set of values used when creating a process, as shown in the following attributes <br/> startinfo. filename = "cmd.exe "; // set the command <SPAN class = 'wp _ keywordlink '> Program </span> <br/> // The following method is used to hide the CMD window <br/> startinfo. arguments = "/C" + doscommand; // set the parameter. To enter the character of the command program, "/C" indicates that the command is exited immediately after execution. <br/> startinfo. useshellexecute = false; // do not use the System Shell to start <br/> startinf O. redirectstandardinput = false; // do not redirect the input <br/> startinfo. redirectstandardoutput = true; // The redirected output is not displayed on the DOS console by default. <br/> startinfo. createnowindow = true; // do not create a window <br/> process. startinfo = startinfo; </P> <p> try <br/> {<br/> If (process. start () // start the process <br/>{< br/> If (outtime = 0) <br/> {process. waitforexit () ;}< br/> else <br/> {process. waitforexit (outtime) ;}< br/> output = process. standardoutput. Readtoend (); // read process output <br/>}< br/> catch <br/>{</P> <p >}< br/> finally <BR/>{< br/> If (process! = NULL) <br/> {process. Close () ;}</P> <p >}< br/> return output; <br/>}

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.