C # use the process class to call external programs and execute doscommands

Source: Internet
Author: User

The processclass in csung allows convenient calling of external programs, so that we can use the cmd.exe program.
Add the parameter "/c" + command to execute a doscommand
(/C indicates that the domain name will be shut down after the specified command is executed. .exe/K6)

[Csharp]
Class ExampleOfLegalsoft
{
Private string RunCmd (string command)
{
// A Process class of the instance to start an independent Process
Process p = new Process ();
// The Process class has a StartInfo attribute. This is the ProcessStartInfo class, which includes some attributes and Methods. Below we use several of its attributes:
P. StartInfo. FileName = "cmd.exe"; // set the program name
P. StartInfo. Arguments = "/c" + command; // set program execution Parameters
P. StartInfo. UseShellExecute = false; // close Shell usage
P. StartInfo. RedirectStandardInput = true; // redirect standard input
P. StartInfo. RedirectStandardOutput = true; // redirect standard output
P. StartInfo. RedirectStandardError = true; // redirect error output
P. StartInfo. CreateNoWindow = true; // you can specify not to display a window.
P. Start (); // Start www.2cto.com
// P. StandardInput. WriteLine (command); // you can enter the command to be executed in this way.
// P. StandardInput. WriteLine ("exit"); // However, remember to add Exit. Otherwise, the next line of code will go live when it is executed.
Return p. StandardOutput. ReadToEnd (); // obtain the command execution result from the output stream
}
}


Author: stoneson

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.