问题
I need to build an simple console application that accepts a pipe as input. We are running Windows Server 2012. The data is coming from another application that is going to "pipe" the input to this application. I have an understanding of pipes from a Linux perspective but do not understand them well from a Windows perspective.
My best guess is that I need to send input to my application like this: C:\app.exe < test.txt
When using the '<' character my current understanding is that it converts test.txt to a stream and will pass in a pointer.
My question is, can anyone give me an example of how to receive a stream pointer, or something equivalent to a pipe in windows in my application, so that I can read the input?
回答1:
When you use < and > with an application, the standard input and output streams (screen/keyboard interface) is replaced by a file stream.
You can use the regular Console.Read and Console.ReadLine commands to read from the stream specified by the < directive, or use Console.In which is a TextReader.
Similarly Console.Write and Console.WriteLine can be used to write to the output stream specified by the > directive, or Console.Out which is a TextWriter.
If you use the | pipe directive, for example myapp.exe | sort, the output stream of the first program goes into the input stream of the next program.
来源:https://stackoverflow.com/questions/15525641/sending-pipe-arguments-to-a-c-sharp-application