Sending pipe arguments to a C# application

前提是你 提交于 2020-05-08 12:16:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!