标准输入和放大器;输出在.NET [英] Standard Input & Output in .NET

查看:97
本文介绍了标准输入和放大器;输出在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从标准输入读取和写入到标准输出。 System.Diagnostics.Process.StandardInput的MSDN参考并没有帮助,因为它单独启动过程,然后重定向标准输入/输出,但如果该进程已经运行,并要求我的应用程序给它一些数据。下面是一个例子,使事情有点清晰的:

How can I read from standard input and write to standard output. System.Diagnostics.Process.StandardInput's MSDN reference didn't help as it separately starts the process and then redirects the Standard Input/Output but what If the process is already running and called my Application to feed it some data. Here's an example to make things a bit clear:

我只是使用Unix的管道,即在Cygwin中,基本上只是读取标准输入并打印到标准输出cat命令(在Linux环境一样适用于Windows)。以下是命令:

I am simply using Unix pipes i.e. cat command in cygwin (A Linux like Environment for windows) that basically just reads standard input and print to standard output. following is the command:

%的猫输入/ sample.txt的| SRC / CSHARP / maptest

% cat input/sample.txt | src/csharp/maptest

但是,这并不似乎工作。

But that doesn't seems to work.

如果有人知道红宝石在这里,因为我没有在这里就是我想在C#中做同样的:

If some one know ruby here as i don't here's what i want to do the same in C#:

#!/usr/bin/env ruby 

STDIN.each_line 
do |line| 
some code here 
end

这就是我要完成在C#或vb.net一些Python相当于code:

And here's some python equivalent code that i want to accomplish in c# or vb.net:

#!/usr/bin/env python 

import re 
import sys 

for line in sys.stdin: 
val = line.strip() 

任何解决方案?

Any solutions?

在此先感谢。

推荐答案

指针为 System.IO.Pipes 文件是一个红色的鲱鱼在这里。

Pointers to documentation for System.IO.Pipes are a red herring here.

如果你只是想有个过程管它的标准输出到另一个进程的标准输入的能力,那么这个工作一样,你会期望提供的目标进程写入从标准输入读取输入。例如,我可以做

If you just want the ability to have a process pipe its standard output to another process's standard input, then this works the same as you would expect provided the target process is written to read input from standard input. For example I can do

dir /b /s c:\*.* | findstr exe

要找到我的C的所有可执行文件:驱动器

to find all executable files on my C: drive.

您只需所以做的是建立自己的 maptest 应用程序,使其从标准输入读取,换句话说,它必须接受通过中提到的其他答案Console.Read * 的方法。看到这个previous问题的早期讨论

All you need to do therefore is to build your maptest application so that it reads from standard input, in other words it must accept input via the Console.Read* methods as mentioned in other answers. See this previous question for an earlier discussion

C#控制台接收输入与管道

这篇关于标准输入和放大器;输出在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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