73

I created a windows form solution and in the constructor of a class I called

Console.WriteLine("constructer called")

But I only got the form and not the console.. so where is the output?

Jazimov
  • 12,626
  • 9
  • 52
  • 59
SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139
  • 7
    or if you start your exe from the console and dont want to change your app manifest to be a console app, here is the real solution: http://www.nerdyhearn.com/blog/157/ – v.oddou Feb 24 '14 at 09:08

4 Answers4

90

You should also consider using Debug.WriteLine, that's probably what you're looking for. These statements are written out the trace listeners for your application, and can be viewed in the Output Window of Visual Studio.

Debug.WriteLine("constructor fired");
BrandonZeider
  • 8,014
  • 2
  • 23
  • 20
78

In project settings set application type as Console. Then you will get Console window AND Windows form.

Tomas Voracek
  • 5,886
  • 1
  • 25
  • 41
20

If you run your application in Visual Studio you can see the console output in the output window.

Debug -> Windows -> Output

Note that the preferable way to output diagnostics data from a WinForms application is to use System.Diagnostics.Debug.WriteLine or System.Diagnostics.Trace.WriteLine as they are more configurable how and where you want the output.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • 1
    How do you configure how and where you want the output? I've gone to the debug options window, and under Debugging > Output Window > General Output Settings, "All debug output" is set to "On," and I still can't get anything to show up in the output window using those methods. – Kyle Delaney Jan 23 '17 at 00:26
  • @Albin: I do not think this works with a WinForms application. Not sure why your answer was accepted by so many people because this question concerns WinForms (not console) applications running in Visual Studio. – Jazimov Jan 06 '23 at 14:24
15

As other answers have stated System.Diagnostics.Debug.WriteLine is the right call for debugging messages. But to answer your question:

From a Winforms application you can invoke a console window for interaction like this:

using System.Runtime.InteropServices;

...

void MyConsoleHandler()
{
    if (AllocConsole())
    {
        Console.Out.WriteLine("Input some text here: ");
        string UserInput = Console.In.ReadLine();

        FreeConsole();
    }
}


[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();

I sometimes use this to raise a command prompt instead of application windows when given certain switches on opening.

There's some more ideas in this similar question if anyone needs it:
What is the Purpose of Console.WriteLine() in Winforms

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • 1
    This is exactly what I've been looking for. Thank you very much for posting this even though nobody specifically asked for it. – Mr. TA Jul 18 '21 at 19:25