c#重定向(管道)进程输出到另一个进程 [英] c# redirect (pipe) process output to another process

查看:43
本文介绍了c#重定向(管道)进程输出到另一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Process 类在 c# 中运行一个进程.

I am trying to run a process in c# using the Process class.

Process p1  = new process();
p1.startinfo.filename =  "xyz.exe";
p1.startinfo.arguments = //i am building it based on user's input.
p1.start();

因此,根据用户输入,我正在构建参数值.现在我有一个案例,我必须将 p1 的输出通过管道传输到另一个进程,比如 grep.所以我基本上尝试了这个

So based on user input i am building the argument value. Now i have a case where i have to pipe the output of p1 to another process say grep. so i basically tried this

p1.startinfo.arguments = "-info |grep 1234" ;

p1.startinfo.arguments = "-info |grep 1234" ;

我想要的是类似于 xyz.exe -info|grep 1234

what i intended is something like xyz.exe -info|grep 1234

但这似乎在.net中不起作用..我实际上可以创建另一个进程变量并将grep"作为一个单独的进程运行..但我想知道是否有任何方法可以像上面尝试的那样做..

but this doesn't seem to work in .net .. I can actually create another process variable and run "grep" as a separate process.. But i was wondering if there is any way to do as iam trying out above..

推荐答案

更简单的方法是使用 cmd 作为您的进程.

The much easier way would be to do just use cmd as your process.

Process test = new Process();
test.StartInfo.FileName = "cmd";
test.StartInfo.Arguments = @"/C ""echo testing | grep test""";
test.Start();

您可以像任何正常过程一样捕获输出或其他任何您想要的内容.这只是我构建的一个快速测试,但它可以将测试输出到控制台,所以我希望这适用于您计划使用管道做的任何其他事情.如果您希望命令保持打开状态,请使用/K 而不是/C,一旦进程完成,窗口将不会关闭.

You can capture the output or whatever else you want like any normal process then. This was just a quick test I built, but it works outputting testing to the console so I would expect this would work for anything else you plan on doing with the piping. If you want the command to stay open then use /K instead of /C and the window will not close once the process finishes.

这篇关于c#重定向(管道)进程输出到另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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