Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# redirect (pipe) process output to another process

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();

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" ;

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

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..

like image 398
FatDaemon Avatar asked Aug 17 '09 22:08

FatDaemon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

You can use CliWrap to make piping really easy, without the overhead of a console, and also completely x-platform:

var output = File.Create("output.txt"); // stream is just one of the options

var cmd = Cli.Wrap("xyz.exe") | output;
await cmd.ExecuteAsync();
like image 118
Tyrrrz Avatar answered Oct 03 '22 17:10

Tyrrrz


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();

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.

like image 21
Mark Avatar answered Oct 04 '22 17:10

Mark