C # Call the CMD program,

Source: Internet
Author: User

C # Call the CMD program,

Query the hard disk status. The two command line program calls are a little different. record it.

1. Use ProcessStartInfo to configure parameter calls.

This is directly input with parameters in CMD. It should be noted that error output should be processed, and errors and standard output may exist at the same time.

ProcessStartInfo start = new ProcessStartInfo ("openfiles"); // set the running command line program. If it is not a system environment variable, enter the full path start. arguments = "/Query/V/NH/S" + host + "/U" + user + "/P" + passWord; // The start parameter of the command line program. createNoWindow = true; // The doscommand line window start is not displayed. redirectStandardOutput = true; // standard output start. redirectStandardInput = true; // standard input start. redirectStandardError = true; // error output start. useShellExecute = false; // specify to directly start the called program itself Process p = Process instead of using the system shell. start (start); StreamReader reader = p. standardOutput; // capture the output stream errorMSG = p. standardError. readLine (); // read error output string result = reader. readToEnd (); // read standard output p. waitForExit (); // wait for the program to run and exit process p. close (); // Close the reader process. close (); // Close the stream

 

2. Input Using the Process... StandardInput. WriteLine method.

You can double-click to open the "waiting for input" button. Note that you must enter exit to exit before reading the output. In this case, you can write the input in a separate file and then input the file as a parameter. Unless you need to modify the input frequently for testing, will the write parameter be writable in the file?

Process p = new Process (); // new instance of Process class p. startInfo. useShellExecute = false; // do not start a new shell p. startInfo. redirectStandardOutput = true; // Redirects the on screen results p. startInfo. fileName = "diskpart.exe"; // executable to run p. startInfo. redirectStandardInput = true; // Redirects the input commands p. start (); // Starts the process p. standardInput. writeLine ("List Volume "); // Issues commands to diskpart p. standardInput. writeLine ("exit"); // The StreamReader reader = p. standardOutput; // capture the output stream string output = null; // read a row at a time while (! Reader. endOfStream) {output + = Environment. newLine;} p. waitForExit (); // Waits for the exe to finish p. close (); // Close the reader process. close (); // Close the stream

 

Refer:
Using DiskPart with C #
Https://ndswanson.wordpress.com/2014/08/12/using-diskpart-with-c/

 

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.