usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Diagnostics;namespaceautoapk{/// <summary> ///a summary description of CMD. /// </summary> Public classCMD {PrivateProcess proc =NULL; /// <summary> ///Construction Method/// </summary> PublicCmd () {proc=NewProcess (); } /// <summary> ///executing a CMD statement/// </summary> /// <param name= "cmd" >the cmd command to execute</param> Public stringRuncmd (stringcmd) {proc. Startinfo.createnowindow=true; Proc. Startinfo.filename="Cmd.exe"; Proc. Startinfo.useshellexecute=false; Proc. Startinfo.redirectstandarderror=true; Proc. Startinfo.redirectstandardinput=true; Proc. Startinfo.redirectstandardoutput=true; Proc. Start (); Proc. Standardinput.writeline (CMD); Proc. Standardinput.writeline ("Exit"); stringOUTSTR =Proc. Standardoutput.readtoend (); Proc. Close (); returnoutstr; } /// <summary> ///Open the software and execute the command/// </summary> /// <param name= "ProgramName" >software path plus name (. exe file)</param> /// <param name= "cmd" >the command to execute</param> Public voidRunprogram (stringProgramName,stringcmd) {Process proc=NewProcess (); Proc. Startinfo.createnowindow=true; Proc. Startinfo.filename=ProgramName; Proc. Startinfo.useshellexecute=false; Proc. Startinfo.redirectstandarderror=true; Proc. Startinfo.redirectstandardinput=true; Proc. Startinfo.redirectstandardoutput=true; Proc. Start (); if(CMD. Length! =0) {Proc. Standardinput.writeline (CMD); } proc. Close (); } /// <summary> ///Open Software/// </summary> /// <param name= "ProgramName" >software path plus name (. exe file)</param> Public voidRunprogram (stringprogramname) { This. Runprogram (ProgramName,""); } }}
The calling code is as follows
New CMD (); = C.runcmd (TxtCmd.Text.Trim ());
Well, with this method, we can easily implement some functions in the future.
And no need to pay a lot of heat to do some things, like I shut down the computer
Commands can be executed directly
Shutdown-s-T 3600-c "comments to display"-F
The meaning of each parameter:-S action for shutdown
-T 3600 delay 3,600 seconds shutdown
-C "comments to display"
-F Force shutdown of all programs without displaying any warnings
If you want more commands, enter help directly
The effect is as follows
Microsoft Windows [version6.1.7601] Copyright (c) theMicrosoft Corporation. All rights reserved. E:\Sufei\itemcode\AutoApk\AutoApk\bin\Debug>Help for more information about a command, type assoc to display or modify the File name extension Association. ATTRIB Display or change file properties. Break set or clear an extended CTRL+C check. BCDEDIT set the properties in the startup database to control the boot load. CACLS displays or modifies the Access Control List (ACL) of the file. Call calls this one from another batch program. The CD displays the name of the current directory or changes it. CHCP Displays or sets the number of active code pages. CHDIR displays the name of the current directory or changes it. CHKDSK checks the disk and displays the status report. CHKNTFS Displays or modifies the startup 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 contents 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 of the files to another location. Date Displays or sets dates. DEL deletes at least one file. DIR displays the files and subdirectories in a directory. DISKCOMP compares the contents of two floppy disks. DISKCOPY Copy the contents of one floppy disk to another. DISKPART displays or configures disk partition properties. DOSKEY Edit the command line, invoke the Windows command, and create a macro. Driverquery Displays the current device driver status and properties. echo Displays the message, or turns the command back on or off. Endlocal to end localization of environment changes in batch files. ERASE Delete one or more files. Exit CMD. EXE Program (command interpreter). FC compares two files or two sets of files and displays the differences between them. Find searches one or more files for a text string. FINDSTR searches for strings in multiple files. For each file in a set of files, run a specified command. Format the disk in order to follow Windows uses. FSUTIL displays or configures the properties of the file system. FTYPE displays or modifies the file types that are associated with the file name extension. GOTO points The Windows command interpreter to a labeled row in the batch program. GPRESULT Displays Group Policy information for the machine or user. GRAFTABL enable Windows to display the extended character set in graphics mode. Help provides information about Windows commands. ICACLS displays, modifies, backs up, or restores ACLs for files and directories. IF a conditional process is performed in a batch program. Label creates, changes, or deletes a disk's volume label. MD to create a directory. MKDIR Create a directory. MKLINK Create Symbolic links and hard links mode to configure system devices. More display output per screen. Move moves one or more files from one directory to another. OPENFILES displays files opened by remote users for file sharing. Path displays or sets the search path for the executable file. Pause stops processing of the batch file and displays information. POPD restores the previous value of the current directory saved by PUSHD. Print prints a text file. PROMPT Change Windows command prompt. PUSHD Save the current directory and make changes to it. RD Delete directory. The RECOVER recovers readable information from the damaged disk. REM records batch files or CONFIG. The comment in SYS. REN renames the file. RENAME Rename the file. Replace replaces the file. RMDIR Delete the directory. ROBOCOPY the Advanced utility set to copy files and the directory tree displays, sets, or removes Windows environment variables. SETLOCAL began to change the localization of the environment with batch files. SC Display or configurationServices (background processing). SCHTASKS schedule commands and programs to run on a single computer as scheduled. SHIFT adjusts the position of replaceable parameters in the batch file. SHUTDOWN allow the machine to shut down properly locally or remotely. Sort sorts the inputs. START opens a separate window to run the specified program or command. SUBST associates the drive letter with the path. SYSTEMINFO displays the specific properties and configuration of the machine. TASKLIST displays all currently running tasks that include the service. TASKKILL terminates a running process or application. Time displays or sets the system times. The TITLE sets CMD. The window caption of the EXE session. Tree displays the directory structure of the initiator or path graphically. TYPE Displays the contents of the text file. VER shows the version of Windows. VERIFY tells Windows to verify that the file is written to disk correctly. VOL Displays the disk volume label and serial number. XCOPY copies files and directory trees. WMIC displays WMI information in an interactive command shell. For more information about tools, see the command-line reference in the online Help.
C # executes the cmd command and receives the implementation of the returned result