[C #] Write read console data using process Standardinput and StandardOutput

Source: Internet
Author: User

This article for the original article, source code for the original code, such as reprint/copy, please in the page/code location clearly marked the original name, author and website, thank you!

Development tools: VS2017

Language: C #

dotnet version:. Net FrameWork 4.0 and above

To demonstrate the use of the program to read the console data, you now need to write a console program, the code is as follows:

usingSystem;namespacetest{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Hello world!"); stringContent =Console.ReadLine (); if(content. ToUpper () = ="TEST") {Console.WriteLine ("You have type correctly. \r\ncongratulation!"); }            Else{Console.WriteLine ("Sorry,uncorrectly!\r\nsee you later."); }        }    }}

The purpose of this code is to:

1) in console output "Hello world!" ;

2) when the output "Test" (regardless of case), the output:

You have type correctly.

congratulation!

3) When the output is other, the output:

sorry,uncorrectly!

See you later.

The above is an essential application, if you do not use the program, you can use the System32 folder under the Cmd.exe as a console application;

Second, the above-generated program Test.exe copied to the D packing directory;

Third, create a new console application (Mainconsoleapp) to manipulate the previous console application and write the following code:

usingSystem;usingSystem.Diagnostics;namespacemainconsoleapp{classProgram {Static voidMain (string[] args) {Process proc=NewProcess {startinfo=NewProcessStartInfo {FileName=@"D:\test.exe", UseShellExecute=false, CreateNoWindow=true, Redirectstandardinput=true, Redirectstandardoutput=true                }            }; Proc.            Start (); Proc. Standardinput.writeline ("Test"); stringline =NULL;  while(!Proc. Standardoutput.endofstream) { line+ = Proc. Standardoutput.readline () +Environment.NewLine;            } Console.WriteLine (line); Proc.            WaitForExit (); //Start AgainProc.            Start (); Proc. Standardinput.writeline ("T"); stringContent =Proc.            Standardoutput.readtoend ();            Console.WriteLine (content); Proc.            WaitForExit ();        Console.readkey (); }    }}

Introduction to Code functions:

Processstartinfo.filename: The name of the application to start;

Processstartinfo.useshellexecute: To redirect the Io stream, the process object must have the UseShellExecute property set to false;

Processstartinfo.createnowindow: Whether to create the application in a mode with no form, the default is False, which is to have a form, such as true, to hide the form. It is also possible to not set this value here;

Processstartinfo.redirectstandardinput: redirect of standard input stream, redirect to process, we can write data to standard stream through Process.StandardInput.WriteLine;

Processstartinfo.redirectstandardoutput: Instead of redirectstandardinput, this is the redirection of the standard output stream, which we can Process.RedirectStandardOutput.ReadLin E and other methods to read the standard stream data.

Process.StandardInput.WriteLine: Writes data to the standard stream;

Process.StandardOutput.ReadLine (or ReadToEnd): reads data from a standard stream.

Four, the program will write to Test.exe data, and read back from the Test.exe data, the use of redirection technology, the results of the operation are as follows:

We can see that the original is the Test.exe program, all the data operations are redirected, directly in the MainConsoleApp.exe input and output.

This technique can be used not only for the above examples, but also for other programs with console input and output, such as Cmd.exe, where you can experiment without an example.

[C #] Write read console data using process Standardinput and StandardOutput

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.