Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect process output C#

I would like to redirect the Process's standard output to a string for later parsing. I would also like to see the output on the screen, while the process is running, and not only when it finishes it's run.

Is that even possible?

like image 960
Idanis Avatar asked Sep 03 '13 09:09

Idanis


People also ask

What is output redirection in C?

Before the C shell executes a command, it scans the command line for redirection characters. These special notations direct the shell to redirect input and output. You can redirect the standard input and output of a command with the following syntax statements: Item.

How do I redirect the output of an already running process?

The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.

How do I redirect standard output to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

Why do we use dup2?

The dup2() system function is used to create a copy of an existing file descriptor. In Linux, there are 3 standard file descriptors. They are: stdin: This is the standard input file descriptor.


2 Answers

Use RedirectStandardOutput.

Sample from MSDN:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Also see OutputDataReceived and BeginOutputReadLine() for an alternative to ReadToEnd(), that will better fulfill your "see output while the process is running" requirement.

like image 98
nmaier Avatar answered Oct 04 '22 21:10

nmaier


If you want to execute an exe from your c# application and get the output from it then you can use the below code

System.Diagnostics.Process p = new System.Diagnostics.Process();            

p.StartInfo.FileName = "PATH TO YOUR FILE";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;              

p.EnableRaisingEvents = true;
p.Start();            
svgText = p.StandardOutput.ReadToEnd();

using(StreamReader s = p.StandardError)
{
    string error = s.ReadToEnd();
    p.WaitForExit(20000);
}

Don't forgete to write p.EnableRaisingEvents = true;

like image 40
Ronak Patel Avatar answered Oct 04 '22 20:10

Ronak Patel