1

I am trying to develope a windows application to display console window as output.

If i changed output type in properties to "console Application" both the console window and form showing. But i need to show only form window first, when i click a button then only the console window need to display the output value.

Please guide me...

Thanks in advance.

Bala
  • 102
  • 8
  • If you want only the console output, then why have you created a Windows Forms project in the first place instead of a Console Application project? – Thorkil Holm-Jacobsen Oct 25 '17 at 10:50
  • Use `Process.Start` on "cmd.exe" and pass it a command line. If you need more functionality than that, you must explain better what you want. – DonBoitnott Oct 25 '17 at 10:51
  • Have a look at this [related post](https://stackoverflow.com/questions/472282/show-console-in-windows-application) – Axel Kemper Oct 25 '17 at 10:55

3 Answers3

0

Use NativeMethods.AllocConsole(); to allocate a console and I/O with it.

Use NativeMethods.FreeConsole(); to close and free this console.

0

Each process can have only at most one console associated with it. There are several paths you can take: either you hide the console window immediately on startup and show it later (via ShowWindow), or you leave the project as a window application, and create the console manually using AllocConsole later.

In the case you wanted multiple consoles, you can e.g. create a dummy process (cmd) and attach your process to its console, using AttachConsole. The managed way would be to use remoting or other techniques to communicate with the dummy process (with your own implementation), and print texts through that.

IS4
  • 11,945
  • 2
  • 47
  • 86
0

You wanna output the Console.WriteLines of a Windows Forms app inside the form?
In that case just use Console.SetOut(Stream) and pass in something you're wrapping so you can also output it somewhere else.
Another option is to switch all your Console.WriteLines to Trace.WriteLine and add a TraceListener somewhere that does what you want.

Quibblesome
  • 25,225
  • 10
  • 61
  • 100