Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.start: how to get the output?

I would like to run an external command line program from my Mono/.NET app. For example, I would like to run mencoder. Is it possible:

  1. To get the command line shell output, and write it on my text box?
  2. To get the numerical value to show a progress bar with time elapsed?
like image 898
stighy Avatar asked Nov 27 '10 13:11

stighy


People also ask

What is stdout in C#?

Stdout means "Standard Output".

What is a Process in c#?

C# Process class provides Start method for launching an exe from code. The Process class is in the System. Diagnostics namespace that has methods to run a .exe file to see any document or a webpage. The Process class provides Start methods for launching another application in the C# Programming.


2 Answers

When you create your Process object set StartInfo appropriately:

var proc = new Process  {     StartInfo = new ProcessStartInfo     {         FileName = "program.exe",         Arguments = "command line arguments to your executable",         UseShellExecute = false,         RedirectStandardOutput = true,         CreateNoWindow = true     } }; 

then start the process and read from it:

proc.Start(); while (!proc.StandardOutput.EndOfStream) {     string line = proc.StandardOutput.ReadLine();     // do something with line } 

You can use int.Parse() or int.TryParse() to convert the strings to numeric values. You may have to do some string manipulation first if there are invalid numeric characters in the strings you read.

like image 166
Ferruccio Avatar answered Oct 07 '22 09:10

Ferruccio


You can process your output synchronously or asynchronously.

1. Synchronous example

static void runCommand() {     Process process = new Process();     process.StartInfo.FileName = "cmd.exe";     process.StartInfo.Arguments = "/c DIR"; // Note the /c command (*)     process.StartInfo.UseShellExecute = false;     process.StartInfo.RedirectStandardOutput = true;     process.StartInfo.RedirectStandardError = true;     process.Start();     //* Read the output (or the error)     string output = process.StandardOutput.ReadToEnd();     Console.WriteLine(output);     string err = process.StandardError.ReadToEnd();     Console.WriteLine(err);     process.WaitForExit(); } 

Note that it's better to process both output and errors: they must be handled separately.

(*) For some commands (here StartInfo.Arguments) you must add the /c directive, otherwise the process freezes in the WaitForExit().

2. Asynchronous example

static void runCommand()  {     //* Create your Process     Process process = new Process();     process.StartInfo.FileName = "cmd.exe";     process.StartInfo.Arguments = "/c DIR";     process.StartInfo.UseShellExecute = false;     process.StartInfo.RedirectStandardOutput = true;     process.StartInfo.RedirectStandardError = true;     //* Set your output and error (asynchronous) handlers     process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);     process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);     //* Start process and handlers     process.Start();     process.BeginOutputReadLine();     process.BeginErrorReadLine();     process.WaitForExit(); }  static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)  {     //* Do your stuff with the output (write to console/log/StringBuilder)     Console.WriteLine(outLine.Data); } 

If you don't need to do complicate operations with the output, you can bypass the OutputHandler method, just adding the handlers directly inline:

//* Set your output and error (asynchronous) handlers process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data); process.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data); 
like image 23
T30 Avatar answered Oct 07 '22 11:10

T30