将管道参数发送到C#应用程序 [英] Sending pipe arguments to a C# application

查看:106
本文介绍了将管道参数发送到C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个简单的控制台应用程序,该应用程序接受管道作为输入.我们正在运行Windows Server2012.数据来自另一个应用程序,该应用程序会将输入管道"到该应用程序.我从Linux的角度了解管道,但从Windows的角度却不太了解.

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.

我的最佳猜测是,我需要像这样将输入发送到我的应用程序: C:\ app.exe< test.txt

My best guess is that I need to send input to my application like this: C:\app.exe < test.txt

使用<"时我目前的理解是,该字符将test.txt转换为流,并将传入指针.

When using the '<' character my current understanding is that it converts test.txt to a stream and will pass in a pointer.

我的问题是,任何人都可以给我一个如何接收流指针或与应用程序中Windows中的管道等效的东西的示例,以便我可以读取输入吗?

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?

推荐答案

在应用程序中使用<>时,标准输入和输出流(屏幕/键盘界面)被文件流替换.

When you use < and > with an application, the standard input and output streams (screen/keyboard interface) is replaced by a file stream.

您可以使用常规的Console.ReadConsole.ReadLine命令从<指令指定的流中读取,也可以使用Console.In(即TextReader).

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.

类似地,可以使用Console.WriteConsole.WriteLine写入由>指令或Console.Out(即TextWriter)指定的输出流.

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.

如果使用|管道指令(例如myapp.exe | sort),则第一个程序的输出流将进入下一个程序的输入流.

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.

这篇关于将管道参数发送到C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆