80

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?

2
  • 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: nerdyhearn.com/blog/157
    – v.oddou
    Commented Feb 24, 2014 at 9:08
  • Why tell me to use Debug, if I want it on the end-user's Console while not running Studio, to service a -verbose option??? Downvoted 1st answer
    – Martin
    Commented Apr 28 at 15:46

4 Answers 4

97

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");
6
  • 10
    I keep trying this, but nothing ever shows up in the output window except the usual build started build succeeded stuff. Commented Jan 23, 2017 at 0:23
  • 1
    @KyleDelaney did you switch from "Build" to "Debug" output using the drop down?
    – Mr. TA
    Commented Jul 18, 2021 at 19:25
  • @Mr.TA - I don't know, it was over 4 years ago Commented Jul 19, 2021 at 21:33
  • @KyleDelaney What about now? :) Commented Jun 18, 2022 at 4:54
  • Works only in Debug configuration. Commented Aug 31, 2022 at 10:17
79

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

1
  • Which line has to change? <OutputType>WinExe</OutputType> <TargetFramework>net8.0-windows</TargetFramework> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> <ImplicitUsings>enable</ImplicitUsings> <Platforms>AnyCPU</Platforms> <ApplicationIcon>speaker-png-icon-6.ico</ApplicationIcon>
    – Martin
    Commented Apr 28 at 15:48
19

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.

3
  • 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. Commented Jan 23, 2017 at 0:26
  • 1
    @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
    Commented Jan 6, 2023 at 14:24
  • In a Winform there is NO output in Output Window, Sorry!!
    – Martin
    Commented Apr 28 at 15:49
16

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

1
  • 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
    Commented Jul 18, 2021 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.