C # Run cmd. EXE in the code to execute the Dir command and receive the returned value.

Source: Internet
Author: User
PROCESS p = new process (); p. startinfo. filename = "cmd.exe"; // name of the program to be executed p. startinfo. useshellexecute = false; p. startinfo. redirectstandardinput = true; // the input information from the caller may be accepted. startinfo. redirectstandardoutput = true; // get the output information by the calling program p. startinfo. createnowindow = true; // do not display the program window p. start (); // start the program // send the input information to the CMD window: P. standardinput. writeline ("dir"); // obtain the output information of the CMD window: streamreader sr = P. standardoutput; string soutput = sr. readline ();// The following two sentences are crucial. If this parameter is not set, the system will crash because the stream will still receive a read row after reading the last row. Because there is no data for the stream to read, it will always wait.Threads. Sleep (5000); p. Kill (); While (soutput! = NULL) {This. textbox1.text + = soutput + "\ r \ n"; if (! Sr. endofstream) soutput = Sr. Readline (); else soutput = NULL;} Sr. Close (); p. Close ();

The above are just some simple problems I encountered in the project. They are published for your reference.

Related Article

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.