1

Fairly simple question, but I can't seem to find an answer.

What's the C# equivalent to doing echo "foo" > CON in a Windows batch script?

Note that I have to specifically output to the console stream (CON). I've found lots of answers addressing how to redirect it when calling Process.Start(), but my process is launched and STDOUT is redirected outside of my control.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • http://msdn.microsoft.com/en-us/library/windows/desktop/ms682075(v=vs.85).aspx might help, with `Console.SetOut()` and some P/Invoke. – SLaks Nov 04 '13 at 04:18
  • [AttachConole](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx) ? – Alexei Levenkov Nov 04 '13 at 04:19
  • I'm just boggled that this is so hard for me to find an answer to. It's *trivial* in a shell script, so it feels like it should be almost as easy in a stronger language... A P/Invoke could probably do it, but that feels like trying to use a sledgehammer on a tiny nail. – Bobson Nov 04 '13 at 04:21

2 Answers2

0

You can try using Console.OpenStandardOutput to open the standard output stream, wrap that in a TextWriter and then use Console.SetOut to reset it.

Kyle
  • 6,500
  • 2
  • 31
  • 41
  • Doesn't that just get the existing `STDOUT`? – Bobson Nov 04 '13 at 04:53
  • @Bobson The documentation says it permits to reacquire the standard output stream. I'm not sure whether this will allow you to "undo" a redirect that's occurring outside of your application. That standard output redirect may simply be entirely out of your control (at least without P/Invoking some Win32 function, but I'm not very familiar with that). – Kyle Nov 04 '13 at 05:01
  • Just tried it. Didn't get any output. Thanks for giving it a shot, though. – Bobson Nov 04 '13 at 05:16
-1

The WriteConsoleOutput() API function will do this.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964