Just an FYI this question has been re-edited to be more concise.
I am working with .NET (whether help is in C# or VB, it doesn't matter). I have a console application, something basic:
using System;
namespace ConsolePrinter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Output");
Console.ReadLine();
}
}
}
In my Winforms application, I need to redirect standard error and standard input, but NOT standard output. Standard output should continue to display in the console window. By making a simple Winforms project and starting the above console EXE as a Process object, I can still view the console's output within the summoned console. As soon as I redirect standard error, the output disappears from the console window and all that displays is a blank console.
Here is an example Winforms project:
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace winformstest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process proc = new Process();
proc.StartInfo.FileName = "ConsolePrinter.exe";
//proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
}
}
}
This code works as intended. Uncommenting that commented line provides the error I'm talking about.
I have tried just attaching forms to a console project, but unfortunately I need to show and hide the console frequently, so starting the console application as a process is ideal. Using Win32 API was also a thought, but the user should not be able to close the entire application by closing the console, which AllocConsole() and other Win32 API calls would cause as a bi-product of their use.
Any help is appreciated, thank you!
It turns out that the reason I had a hard time reproducing this was that it wasn't a code problem at all - it was how the code was being run.
If you run the program in a debugger, for some reason setting RedirectStandardError
to true
changes the behaviour, as noted. Outside a debugger - launching the app from the command line, or Explorer, or in Visual Studio via "Start without debugging" - the output appears as desired.
The simplest way to address this is to just live without debugging when you need the output. That may be awkward, but it's likely to be simpler than the alternatives.
Another option would be to redirect everything and build your own mini-console within your WinForms app, of course. That would be neater and more generally user friendly - no extra windows popping up all over the place - but more work. Probably worth considering if this is going to be a long-term app used frequently by many people, but not worth doing otherwise.
See more on this question at Stackoverflow