Redirect console output to a text field in a separate program

I am developing a Windows Forms application that requires me to invoke a separate program to complete a task. The program is a console application, and I need to redirect standard output from the console to the TextBox in my program.

I have no problem running the program from my application, but I don’t know how to redirect the output to my application. I need to capture the output while the program is running using events.

The console program is not intended to stop until my application stops and the text changes randomly. What I'm trying to do is simply pull the console out of the console to call the event handler, which can then be used to update the TextBox.

I use C # to code the program and use the .NET platform for development. The source application is not a .NET program.

EDIT: Here is a sample code of what I'm trying to do. In my last application, I replaced Console.WriteLine with code to update the TextBox. I tried to set a breakpoint in the event handler, and this has not even been achieved.

void Method() { var p = new Process(); var path = @"C:\ConsoleApp.exe"; p.StartInfo.FileName = path; p.StartInfo.UseShellExecute = false; p.OutputDataReceived += p_OutputDataReceived; p.Start(); } static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) { Console.WriteLine(">>> {0}", e.Data); } 
+49
c # console winforms textbox
Jan 6 '09 at 6:20
source share
5 answers

This works for me:

 void RunWithRedirect(string cmdPath) { var proc = new Process(); proc.StartInfo.FileName = cmdPath; // set up output redirection proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.EnableRaisingEvents = true; proc.StartInfo.CreateNoWindow = true; // see below for output handler proc.ErrorDataReceived += proc_DataReceived; proc.OutputDataReceived += proc_DataReceived; proc.Start(); proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); proc.WaitForExit(); } void proc_DataReceived(object sender, DataReceivedEventArgs e) { // output will be in string e.Data } 
+70
Jan 06 '09 at 6:46
source share
β€” -

You can use the following code

  MemoryStream mem = new MemoryStream(1000); StreamWriter writer = new StreamWriter(mem); Console.SetOut(writer); Assembly assembly = Assembly.LoadFrom(@"C:\ConsoleApp.exe"); assembly.EntryPoint.Invoke(null, null); writer.Close(); string s = Encoding.Default.GetString(mem.ToArray()); mem.Close(); 
+4
Jan 06 '09 at 12:11
source share
+1
Jan 06 '09 at 6:22
source share

I have added some helper methods to the O2 Platform (an open source project) that allow you to easily interact with another process through console output and input (see http://code.google.com/p/o2platform/source/browse /trunk/O2_Scripts/APIs/Windows/CmdExe/CmdExeAPI.cs )

It is also useful to use an API that allows you to view the console output of the current process (in an existing control or pop-up window). See this blog post for more details: http://o2platform.wordpress.com/2011/11/26/api_consoleout-cs-inprocess-capture-of-the-console-output/ (this blog also contains information about how to use console output of new processes)

+1
Nov 26 '11 at 18:01
source share

Thanks to Mark Maxham for the answer that saved me time!

As Jon of All Trades noted, UseShellExecute must be set to false in order to redirect I / O streams, otherwise calling Start() will raise an InvalidOperationException .

Here is my code modification where txtOut is the txtOut field for txtOut only in WPF

 void RunWithRedirect(string cmdargs) { var proc = new Process() { StartInfo = new ProcessStartInfo("cmd.exe", "/k " + cmdargs) { RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }, EnableRaisingEvents = true }; // see below for output handler proc.ErrorDataReceived += proc_DataReceived; proc.OutputDataReceived += proc_DataReceived; proc.Start(); proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); proc.WaitForExit(); } void proc_DataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null) Dispatcher.BeginInvoke(new Action( () => txtOut.Text += (Environment.NewLine + e.Data) )); } 
0
Feb 17 '15 at
source share



All Articles