Using process classes in C # to invoke external programs and execute DOS commands

Source: Internet
Author: User
Tags exit execution
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}



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.