0

Is there a way to write to the console in a WinForm project?

I know that I can change the project type to console and stills be a WinForm, but then if I launch my program from the CMD the execution of the CMD stops until my program exit, this not happens if I launch my app from the console as a Winforms but then I can't write to the console...

So the question is :

How to write to the console on a WinForms or how to don't stop the execution of the CMD when my app is launched from CMD (to avoid the use of "Start /B" on CMD for my application).

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

1 Answers1

3

You can use AllocConsole to attach a process to a new console. After that Console.WriteLine should work as expected. The P/Invoke signature of that function is

[DllImport("kernel32")]
static extern bool AllocConsole();

Note though, that if you launch your program from cmd you will get another console window for your program and re-use of the parent console is impossible. But how I understood you that's your goal anyway.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Thankyou for the information but I'm looking to do it on the same CMD instance, do you know if this is possible?, anyways I've tested the function and the new cmd instance waits for my program to exit :( – ElektroStudios Apr 29 '13 at 04:45
  • @ElektroHacker - [AttachConsole](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx) is right next to AllocConsole... but I assume you are looking for something else. – Alexei Levenkov Apr 29 '13 at 04:54