views:

57

answers:

2

Hello, how can I write DIRECTLY into console, without bothering with stdout in c#? I'm upgrading some old program, which has it's stdout redirected into file (because the output matters), and I need somehow to write directly to console, and that the text won't appear in stdout. Is it possible (without using WinAPI)?

EDIT: I'm aware of the possibility to write to stderr, althrough, is it possible to set cursor position for stderr on console?

+2  A: 

You could write to Console.Error, which, although it can be redirected, is separate from stdout.

is it possible to set cursor position for stderr on console?

Edit: Assuming that stderr has not been redirected, see Console.CursorTop and Console.CursorLeft. There's various other members on the Console class that you might find useful.

To answer your question directly, use the Win32 WriteConsole function. As far as I can see, the .NET framework doesn't have methods for writing directly to the console window.

Tim Robinson
thanks, but see my edit, please.
Yossarian
A: 

If I run your program and redirect its StandardOutput and StandardError, what do you expect to happen to your writes when there is no console?

For this reason, the answer is likely “you can’t” (except perhaps by using crazy hacks, which probably involve Windows API which you said you didn’t want to use).

Think of the console window as nothing more than a UI element that allows the user to view the stdout/stderr of your program (and to provide stdin). It doesn’t really exist for any other purpose.

Timwi