215

I just open a console application and I type

Console.WriteLine("Test");

But the output window doesn't show this. I go to the output window with Ctrl + W, O.

But nothing shows up when I run my program. Am I nuts or is this not supported in Visual Studio 2010 Express?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • 4
    As `Leif` said it disappears before you can see it. Use `Console.ReadKey()` – Xaqron Dec 19 '10 at 00:25
  • See also http://stackoverflow.com/a/1583569/503688 for a mechanism to redirect console output to the debug output window. – yoyo Dec 21 '16 at 22:13

22 Answers22

383

Console.WriteLine writes your output to the console window opened by your application (think black window with white text that appears when you open the Command Prompt.) Try System.Diagnostics.Debug.WriteLine instead.

Adam Maras
  • 26,269
  • 6
  • 65
  • 91
  • 34
    The debug output appears in the Output window (Debug => Windows => Output). Mine wasn't open by default. – Appulus Aug 21 '13 at 10:21
  • 2
    If there is no output in the console window, also check the properties of the application under the Debug tab Start Options to make sure you do not have any command line arguments redirecting the output from the console. – Chad Miller Apr 13 '16 at 16:03
  • And use a tool like https://technet.microsoft.com/en-us/sysinternals/debugview.aspx DebugView (dbgview) to receive the debug messages because they won't appear on the cmd console. – Roger Deep Dec 17 '16 at 09:54
  • Well, if you "start without debugging", you will not see the output....So you have to run your program by "start debugging"...Sounds silly, but I just know that....And don't forget to select "Debug" from the "Show output from" in the output window (In my case, the original tab is "Build") – vainquit Jul 01 '21 at 13:43
  • While this was not the actual problem for mine, But two steps you can check. 1: that you have not redirected the output via `Options->Debugging->General->Redirect all output window text to...`. 2: in my case, i did not have the DEBUG constant defined (check project options) / Result of having it in RELEASE – Angry 84 Aug 29 '21 at 05:01
41

No satisfactory answers were provided.

System.Diagnostics.Debug.WriteLine() will write messages to the Output:debug window, but so much crap is constantly dumped into that window by every process under the sun, it is like finding a needle in a haystack to find your messages.

Console.WriteLine() does not write to any window in Visual Studio. I guess it will only write to the application console if your application creates a console in the first place, i.e. if it is a console application.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Gorbas
  • 1,694
  • 20
  • 16
  • 1
    You could prefix your message with a string, e.g. ">>>" and use [VSColorOutpu](https://marketplace.visualstudio.com/items?itemName=MikeWard-AnnArbor.VSColorOutput) to highlight such lines. – Andreas Feb 09 '21 at 08:48
  • If you right click in the output window you can turn of all of the usual spammers (closing threads, loaded modules, etc). – user3797758 Jan 20 '23 at 14:36
  • In my case, the output appeared in the "Immediate" window. – Marcel Gruber Feb 08 '23 at 17:56
29

Go to properties in you own project in the Solution Explorer window and choose application type and look for Output Type.

Change its value to Console Application.

This will make console screen besides your form. If you close the console screen, your form will be closed too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ibrahim
  • 309
  • 3
  • 2
  • Make my day! I can loop through my LINQ query and write to the console now which is quick cheap way of getting the correct data into the fray. – JustJohn Aug 30 '15 at 06:07
  • 1. It works for VS Enterprise as well. 2. This solution redirects messages not to Output/Debug window of VS, but to a separately opened console window. The latter is often even more convenient. Good! – Alex Fainshtein May 15 '21 at 18:17
24

Perhaps the console is clearing. Try:

Console.WriteLine("Test");
Console.ReadLine();

And it will hopefully stay there until you press enter.

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
8

Or you can debug by CTRL+F5 this will open ConsoleWindow waits after last line executed untill you press key.

Javed Akram
  • 15,024
  • 26
  • 81
  • 118
  • 10
    Well, that's not really 'debugging', that's just launching the application without attaching the debugger. :/ – damian Nov 07 '14 at 14:59
6

It's more than likely because you've used Console in the namespace. For example like this:

namespace XYZApplication.Console
{
    class Program
    {
        static void Main(string[] args)
       {
            //Some code;             
       }
    }
}

Try removing it from the namespace or use the full namespace instead i.e.

   System.Console.Writeline("abc");

The reason is a problem this is an issue is because you might have another clashing namespace. Example:

using System;
using AnotherNamespaceThatContainsAConsoleClass;
nadsy
  • 144
  • 2
  • 8
  • 1
    I didn't have any other Console in my app, but adding `System.` helped. Thanks. – kolenda Aug 30 '17 at 11:12
  • *Why* is using Console a problem? What is the explanation? What are the mechanisms? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/30298179/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 17 '22 at 21:53
  • @PeterMortensen - have updated the response to include why using console is a problem. Thanks for the nudge. – nadsy Dec 28 '22 at 15:54
5

I run into a similar problem while running a unit test. Console.WriteLine() did not write anything into the Visual Studio Output Window.

Using System.Diagnostics.Debug.WriteLine() solved the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Huber
  • 3,052
  • 2
  • 30
  • 42
5

Try Ctrl + F5. It will hold your screen until you press any key.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
5

In a Windows Forms application, both methods,

System.Diagnostics.Debug.WriteLine("my string")

and

System.Console.WriteLine("my string")

write to the output window.

In an ASP.NET Core application, only System.Diagnostics.Debug.WriteLine("my string") writes to the output window.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user6849960
  • 59
  • 1
  • 1
5

The output window isn't the console. Try the methods in System.Diagnostics.Debug

lesscode
  • 6,221
  • 30
  • 58
2

Right click on the project in Solution Explorer and click "Clean".

Now run - press F5.

Make sure the code is as below:

Console.WriteLine("TEST");
Console.ReadLine();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
akash
  • 21
  • 1
2

None of the answers here worked for me!! Most of these people here are stuck in Windows Desktop Application Consoleland. If you are a web developer using ASP.NET in Visual Studio and do not see any console or debug text, here is how to fix that:

  1. Paste the following two tests into your code so it runs both lines. These are tests for the output window:

    System.Console.WriteLine($"hello console!");

    System.Diagnostics.Debug.WriteLine($"hello debugger!");

  2. In Visual Studio choose VIEW > OUTPUT. You will see the results above in this output window after changing two settings below.

  3. When NOT DEBUGGING, in the OUTPUT window at the top under "Show Output From" choose: "YourProjectName - ASP.NET CORE Web Server". Run your code. You should see the "Console" text above.

  4. When DEBUGGING, in the OUTPUT window at the top under "Show Output From" choose: "Debugger". Run your code in debug mode. You should see the "Debug" text above.

Stokely
  • 12,444
  • 2
  • 35
  • 23
1

If you use Ctrl-F5 (start without debugging) it will leave the console window open with a message "Press any key to continue". That's the easiest way to keep the console window from closing so you can see the console output.

1

Go to the Debug menu and select Options and uncheck "Redirect all Output Window text to Immediate Window"

user241636
  • 11
  • 1
1

A workaround I found:

Press Ctrl + Alt + I or navigate to the Debug tab → WindowsImmediate.

In your code write:

Trace.WriteLine("This is one of the workarounds");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

If you are developing a command line application, you can also use Console.ReadLine() at the end of your code to wait for the 'Enter' keypress before closing the console window so that you can read your output.

0
using System.Diagnostics;


Trace.WriteLine("This line will show on Output window"); 
Trace.Flush();

This works on Microsoft Team Explorer for Visual Studio 2013

Refer to microsoft.com

0

The Output window of Visual Studio 2017 have a menu called Show output from, in my case ASP.NET Core Web Server was the option to select in order to see the printed out, I came across this issue since I had it set to Build so I wasn't seeing the printed out lines at runtime.

kuky54
  • 1
  • 1
0

There are 2 possible problems are:

  • Firstly, you have not used the using System which should be before writing code that uses "System class", as Console.WriteLine()
  • Secondly, you have not coded what happens after the Console displays "Test"

The possible solution will be:

using System;

namespace Test
{
    public static Main()
    {
        //Print to the console
        Console.WriteLine("Test");

        //Allow user to read output
        Console.ReadKey();
    }
}

It is also strategic to code Console.Write("Press any key to exit..."); on the line that precedes the Console.ReadKey(); to make the user aware that the program is ending, he/she must press any key to exit.

0

Change the execution mode to "Self Hosted". In this case, the execution console will appear and all the messages will be displayed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Console.Writeline() shows up in the debug output (Debug => Windows => Output).

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Svl2Nuk3
  • 25
  • 1
  • 7
    No, System.Diagnostics.Debug.WriteLine writes to Debug => Windows => Output, but not Console.WriteLine – Vitalii Korsakov Jan 05 '13 at 01:40
  • 1
    The person asking the question is using a Console application, where Console.WriteLine() of course writes to the Console, which is the black coloured application window, looking like a DOS window. Console.WriteLine() only writes to the Debug Output Window, when it is not a Console application. – Peter Huber Jan 22 '16 at 13:57
  • "Console.WriteLine() only writes to the Debug Output Window, when it is not a Console application." Why? – sydd Feb 07 '18 at 08:51
-1

I just had the same problem. What I found on my VS Community 2022 is that you need to add Console.ReadLine(), that way the compiler shows it on the terminal window.

So if you wrote for example "Hello World" and all you saw was your file directory, with Console.Readline() it shows the actual "Hello World" string.

It's the way Visual Studio compilers work. usually on online compilers Console.Readline() will not be needed, might even be taken for an error!

Fander
  • 9
  • 1