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

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

问题描述

喜 我试图使用过程类运行在C#中的过程。

Hi 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作为一个独立的过程。但我想知道是否有办法做到的IAM上面的尝试..

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天全站免登陆