. Net (C #) calls the console program and gets the output result

Source: Internet
Author: User

Although. Net has strong functions, sometimes,Console, command lineCan be ignored and used properlyCommand LineTo get twice the result with half the effort.

Here we will talk about the. NET program as the host, call the command line program, execute some operations, and return the command line output results to the. NET host. The console program we call is not just an example of cmd.exe/command.exe, but a program with standard input and output functions. Most of the common console programs are executed on the CMD command line interface.

In the CMD command lineHelpOr?, You can see the following output :( Taking XP as an example, other operating systems do not have much difference )

Assoc displays or modifies file extension associations.
At is the command and program to be run on the computer.
Attrib displays or changes file attributes.
Set or clear the extended Ctrl + C check.
Cacls displays or modifies the File Access Control List (ACLs ).
Call calls this one from another batch processing program.
CD displays or changes the name of the current directory.
Chcp displays or sets the number of pages of the active code.
Chdir displays or changes the name of the current directory.
Chkdsk checks the disk and displays the status report.
CHKNTFS Displays or modifies the boot time disk check.
CLS clears the screen.
CMD opens another Windows Command Interpreter window.
Color sets the default console foreground and background color.
Comp compares the content of two or two sets of files.
COMPACT Displays or changes the compression of files on NTFS partitions.
CONVERT Converts a fat volume to NTFS. You cannot convert the current drive.
COPY Copies at least one file to another location.
Date: displays or sets the date.
Del deletes at least one file.
DIR Displays files and subdirectories in a directory.
Diskcomp compares two floppy disks.
Diskcopy copies the content of one floppy disk to another.
Doskey: edit the command line, call the Windows Command, and create a macro.
ECHO Displays the message, or explicitly opens or closes the command.
Endlocal ends the localization of Environment Changes in the batch file.
Erase deletes at least one file.
Exit to exit the cmd. exe program (command interpreter ).
FC compares two or two sets of files and displays the differences.
Find searches for text strings in the file.
Findstr searches for strings in the file.
For runs a specified command for each file in a set of files.
Format the disk for use with windows.
FTYPE Displays or modifies the file type associated with the file extension.
Goto points the Windows command interpreter to a specified line in the batch processing program.
Graftabl enables Windows to display extended character sets in image mode.
Help provides help information for Windows commands.
If executes conditional processing in the batch processing program.
Label to create, change, or delete the volume label of a disk.
MD creates a directory.
Create a directory using mkdir.
Mode: configure the system device.
More: display a result screen at a time.
MOVE Moves the file from one directory to another.
Path: displays or sets the search path for executable files.
PAUSE Suspends processing of batch files and displays messages.
Popd restores the previous value of the current directory saved by pushd.
Print a text file.
Prompt to change the Windows command prompt.
Pushd saves the current directory and changes it.
Rd deletes the directory.
Recover restores readable information from the faulty disk.
Rem record comments in a batch file or config. sys.
Rename the file.
Rename the file.
Replace the file.
Delete the rmdir directory.
Set displays, sets, or deletes Windows environment variables.
Setlocal starts localization of Environment Changes in the batch file.
Shift changes the positions of parameters that can be replaced in a batch file.
Sort classifies input.
Start starts another window to run the specified program or command.
SUBST Associates the path with a drive letter.
Time: displays or sets the system time.
Title: Set the title of the cmd. EXE session.
Tree displays the directory structure of the drive or path in graphic mode.
Type: displays the content of a text file.
Server displays the Windows version.
Verify tells windows whether to verify that the file has been correctly written to the disk.
VOL Displays the volume label and serial number of the disk.
Xcopy copies the file and directory tree.

Through the list above, we can see that many complex functions are very easy to complete using command lines (not all commands listed above are only part of them)

The following is an example: To obtain the current system information, you only need to call systeminfo.

 

// Instantiate a process class process cmd = new process (); // obtain the system information, using the console program cmd systeminfo.exe. startinfo. filename = "systeminfo.exe"; // redirects all the standard input and output of CMD.. Net Program cmd. startinfo. useshellexecute = false; // This parameter must be set to false. Otherwise, an exception occurs. cmd. startinfo. redirectstandardinput = true; // standard input cmd. startinfo. redirectstandardoutput = true; // standard output // the command line window CMD is not displayed. startinfo. createnowindow = true; cmd. startinfo. windowstyle = processwindowstyle. hidden; cmd. start (); // start the process // obtain the output // you need to specify the following, // This can be obtained only after the process exits. textbox1.text = cmd. standardoutput. readtoend (); cmd. waitforexit (); // wait until the execution of the console program is completed. cmd. close (); // close the process

Download instance source code

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.