snippetcsharpCritical
Process.start: how to get the output?
Viewed 0 times
howoutputprocessthestartget
Problem
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:
For example, I would like to run mencoder. Is it possible:
- To get the command line shell output, and write it on my text box?
- To get the numerical value to show a progress bar with time elapsed?
Solution
When you create your
then start the process and read from it:
You can use
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.Code Snippets
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
// do something with line
}Context
Stack Overflow Q#4291912, score: 585
Revisions (0)
No revisions yet.