running pipe in c#

M

mrwwli

Hello all,

I can do this in shell

dir /a/b/s | grep something

This is piping the output of first program dir to input of second
program grep .

I would like to implement this in C#. Is there a way to implement pipe
in c# programming?

I can save the output of first program to disk and read from disk as
the input of second program but that will take some time on IO. I want
do this quickly.


thanks

WW
 
P

Peter Duniho

I can do this in shell

dir /a/b/s | grep something

This is piping the output of first program dir to input of second
program grep .

I would like to implement this in C#. Is there a way to implement pipe
in c# programming?

I can save the output of first program to disk and read from disk as
the input of second program but that will take some time on IO. I want
do this quickly.

You can use the System.Diagnostics.Process class, redirecting standard
output of one process and feeding that to the redirected standard input
of another. In this case, you'd have two Process instances, one for
"dir" and one for "grep".

Note that "dir" isn't a program; it's a built-in command for cmd.exe.
So you'll need to specify "cmd.exe" as the actual executable for the
"dir" process, with the appropriate switches to execute the "dir" command.

Another note: I don't have first-hand experience using a program like
"grep" in this way, where the program reads to the end of the stream.
I'm pretty sure you just close the redirected input stream, but you
might have to experiment a bit to find out the exact technique, if that
assumption isn't correct.

Pete
 
J

Jon Skeet [C# MVP]

I can do this in shell

dir /a/b/s | grep something

This is piping the output of first program dir to input of second
program grep .

I would like to implement this in C#. Is there a way to implement pipe
in c# programming?

I can save the output of first program to disk and read from disk as
the input of second program but that will take some time on IO. I want
do this quickly.

Have a look at PowerShell - it's all about that kind of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top