C # Thinking about the process class

Source: Internet
Author: User

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  
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. diagnostics;
  6. Namespace consoleapplication1
  7. {
  8. Class Program
  9. {
  10. Static void main (string [] ARGs)
  11. {
  12. Process = new process ();
  13. String strbatpath = "E:/test. Bat ";
  14. String mess = executebat (strbatpath, process );
  15. Console. writeline (MESS );
  16. Console. readkey ();
  17. }
  18. Private Static string executebat (string strbatpath, process Pro)
  19. // File path; the process for executing the BAT file and returning the execution result
  20. {
  21. String mess = "";
  22. Try
  23. {
  24. Pro. startinfo. useshellexecute = true;
  25. // Strbatpath is the BAT file path
  26. Pro. startinfo. filename = strbatpath;
  27. Pro. startinfo. createnowindow = true;
  28. If (PRO. Start ())
  29. {
  30. // Write a log file
  31. Mess = datetime. Now. tolongdatestring () + "" + strbatpath + "executed successfully ";
  32. }
  33. Else
  34. {
  35. Mess = string. Format ("failed to execute {0}.", strbatpath );
  36. }
  37. }
  38. Catch (exception ex)
  39. {
  40. Mess = ex. message;
  41. }
  42. Finally
  43. {
  44. Pro. Close ();
  45. }
  46. Return mess;
  47. }
  48. }
  49. }

Now I am writing a C # method for reading files.

C # code  
  1. Public static void printfile (string strfilename)
  2. {
  3. Streamreader SRD;
  4. Try
  5. {
  6. SRD = file. opentext (strfilename );
  7. }
  8. Catch (exception E)
  9. {
  10. Console. writeline (E. Message );
  11. Console. writeline ("file not read ");
  12. Return;
  13. }
  14. While (SRD. Peek ()! =-1)
  15. {
  16. String STR = SRD. Readline ();
  17. Console. writeline (STR );
  18. }
  19. Console. writeline ("End of read ");
  20. SRD. Close ();
  21. }
  22. Public static void inputfile (string strfilename)
  23. {
  24. Streamwriter SWT;
  25. Try
  26. {
  27. SWT = file. createtext (strfilename );
  28. }
  29. Catch (exception E)
  30. {
  31. Console. writeline (E. Message );
  32. Console. writeline ("file not write ");
  33. Return;
  34. }
  35. SWT. writeline ("chenhailong ");
  36. SWT. Close ();
  37. }
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.