Here, I will give myself an impression
The program is as follows:
PROCESS p = new process ();
P. startinfo. filename = "cmd.exe"; // set the startup process command
/** Set whether to set standard input/output and standard errors. If these values are set to true
** Useshellexcute must be false */
P. startinfo. useshellexcute = false;
P. startinfo. redirectstanderinput = true;
P. startinfo. redirectstanderoutput = true;
P. startinfo. redirectstandererror = true;
P. startinfo. creatnowindows = true;
P. Start ();
// Enter the ping command in the DOS window. Set the IP address here.
P. standardinput. writeline ("Ping-N 1" + IP );
// Enter the command to exit the window
P .. standardinput. writeline ("exit ");
/** Use readtoend to read the output and assign it to a string value.
** Note that the readtoend command can be executed only after the called program ends.
** If you put this sentence after the above "exit", the program will enter an endless loop */
String output = P. standardoutput. readtoend ();
The main work has been completed, and you can see how to use the input and output of the program to complete some functions.
Here I also wrote an implementation:
Program code
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. text;
- Using system. diagnostics;
- Namespace consoleapplication1
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Process = new process ();
- String strbatpath = "E:/test. Bat ";
- String mess = executebat (strbatpath, process );
- Console. writeline (MESS );
- Console. readkey ();
- }
- Private Static string executebat (string strbatpath, process Pro)
- // File path; the process for executing the BAT file and returning the execution result
- {
- String mess = "";
- Try
- {
- Pro. startinfo. useshellexecute = true;
- // Strbatpath is the BAT file path
- Pro. startinfo. filename = strbatpath;
- Pro. startinfo. createnowindow = true;
- If (PRO. Start ())
- {
- // Write a log file
- Mess = datetime. Now. tolongdatestring () + "" + strbatpath + "executed successfully ";
- }
- Else
- {
- Mess = string. Format ("failed to execute {0}.", strbatpath );
- }
- }
- Catch (exception ex)
- {
- Mess = ex. message;
- }
- Finally
- {
- Pro. Close ();
- }
- Return mess;
- }
- }
- }
Now I am writing a C # method for reading files.
C # code
- Public static void printfile (string strfilename)
- {
- Streamreader SRD;
- Try
- {
- SRD = file. opentext (strfilename );
- }
- Catch (exception E)
- {
- Console. writeline (E. Message );
- Console. writeline ("file not read ");
- Return;
- }
- While (SRD. Peek ()! =-1)
- {
- String STR = SRD. Readline ();
- Console. writeline (STR );
- }
- Console. writeline ("End of read ");
- SRD. Close ();
- }
- Public static void inputfile (string strfilename)
- {
- Streamwriter SWT;
- Try
- {
- SWT = file. createtext (strfilename );
- }
- Catch (exception E)
- {
- Console. writeline (E. Message );
- Console. writeline ("file not write ");
- Return;
- }
- SWT. writeline ("chenhailong ");
- SWT. Close ();
- }