164

I am writing a simple C# program with some outputs (Console.WriteLine("...");). The problem is, each time I run it, I cannot see the program's output in the output window.

The "program output" tag is already checked, and I already redirected all outputs to the intermediate window but to no avail.

How do I enable seeing the program's output?

I don't think the problem lies with my code. I tried running a simple program that just outputs a string and readline "ala hello world" and I am still unable to see any output. The problem is either with me looking for the output in the wrong location or Visual Studio acting out.

The debug.write method also doesn't work.

Using debug.Write, it all works, though it didn't before. Either something bugged out with me before I restarted or I just need to take a break, either way it's all good now. Thanks all for the helpful comments =)

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
r3x
  • 2,125
  • 5
  • 23
  • 39
  • 7
    Visual Studio is covering your console window. Move it to your other monitor. – Hans Passant Mar 14 '11 at 16:29
  • Possible duplicate of [How do I show a console output/window in a forms application?](http://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application) – T.Todua Feb 08 '16 at 08:05

10 Answers10

196

You can use the System.Diagnostics.Debug.Write or System.Runtime.InteropServices method to write messages to the Output Window.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Richard Adnams
  • 3,128
  • 2
  • 22
  • 30
  • 10
    System.Diagnostics.Debug.Write (in case the OP doesn't have the namespace in play) – jonsca Mar 14 '11 at 16:14
  • 4
    Ah true, I'm just to used to hiting control + period :) – Richard Adnams Mar 14 '11 at 16:16
  • jonsca: i have a namespace in play and why can i just use console.writeline, i want my program to write to the console its not only for testing reasons?? – r3x Mar 14 '11 at 16:16
  • 2
    If your application is a console application you can use console.writeline to print to the console window or if you application is a windows form app you can use debug.write. Check this link for abit more info http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/701e02cc-ad77-46b9-b4fc-410bb3ff7a0d – Richard Adnams Mar 14 '11 at 16:22
  • 1
    interesting didn't know that, and yes my app is a windows form app, but no i tried using debug.Write method same problem, it compiles and all is peachy but am unable to see the output anywhere – r3x Mar 14 '11 at 16:28
  • I'm sure you are but just to confirm you are running the application in debug mode via VS (F5 or Debug->Start Debugging)? – Richard Adnams Mar 14 '11 at 16:30
  • I'm getting `Error: identifier "System" is undefined` – CodyBugstein Nov 14 '13 at 14:58
51

Here are a couple of things to check:

  1. For console.Write/WriteLine, your app must be a console application. (right-click the project in Solution Explorer, choose Properties, and look at the "Output Type" combo in the Application Tab -- should be "Console Application" (note, if you really need a windows application or a class library, don't change this to Console App just to get the Console.WriteLine).

  2. You could use System.Diagnostics.Debug.WriteLine to write to the output window (to show the output window in VS, got to View | Output) Note that these writes will only occur in a build where the DEBUG conditional is defined (by default, debug builds define this, and release builds do not)

  3. You could use System.Diagnostics.Trace.Writeline if you want to be able to write to configurable "listeners" in non-debug builds. (by default, this writes to the Output Window in Visual Studio, just like Debug.Writeline)

user3340627
  • 3,023
  • 6
  • 35
  • 80
JMarsch
  • 21,484
  • 15
  • 77
  • 125
  • 2
    I wasn't seeing my output, but then realized that I had to run the program in debug mode (F5) instead of Ctrl + Shift + F5. Thanks! – Travis Heeter Jun 05 '13 at 15:51
  • System.Diagnostics.Trace.Writeline doesn't seem to work though, is there something else I need to configure for this? – Travis Heeter Jun 05 '13 at 15:59
  • 1
    «1» — false statement. Mono shows the output fine for all applications. «2» — ofc. The problem is that the method doesn't outputs to the terminal too. So one can not debug an app on the PC without an IDE. – Hi-Angel Aug 11 '15 at 08:37
34

Add a Console.Read(); at the end of your program. It'll keep the application from closing, and you can see its output that way.

This is a console application I just dug up that stops after processing but before exiting:

class Program
{
    static void Main(string[] args)
    {
        DummyObjectList dol = new DummyObjectList(2);
        dol.Add(new DummyObject("test1", (Decimal)25.36));
        dol.Add(new DummyObject("test2", (Decimal)0.698));
        XmlSerializer dolxs = new XmlSerializer(typeof(DummyObjectList));
        dolxs.Serialize(Console.Out, dol);

        Console.WriteLine(string.Empty);
        Console.WriteLine(string.Empty);

        List<DummyObject> dolist = new List<DummyObject>(2);
        dolist.Add(new DummyObject("test1", (Decimal)25.36));
        dolist.Add(new DummyObject("test2", (Decimal)0.698));
        XmlSerializer dolistxs = new XmlSerializer(typeof(List<DummyObject>));
        dolistxs.Serialize(Console.Out, dolist);
        Console.Read(); //  <--- Right here
    }
}

Alternatively, you can simply add a breakpoint on the last line.

Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25
  • 2
    Console.ReadLine works as well you just have to hit enter to continue where as Read takes any standard key. – Richard Adnams Mar 14 '11 at 16:24
  • its a windows form app, and debug.write nor console.writeline method seem to work – r3x Mar 14 '11 at 16:31
  • Euhm, why are you outputting to the console from a Windows Form app? I suggest outputting on a form or dialgbox instead. Or make a console app... – Vincent Vancalbergh Mar 14 '11 at 16:33
  • yea that was already pointed out to me, its a just a stupid mistake on my part, still thanks for the heads up =) – r3x Mar 14 '11 at 16:38
  • The Console.Read() causes the console window to get focus, and hence appear in front of VS. If you simply put a break point in after a Console.Write() the Console window will show your output but does not have focus so may not be visible. – Nigel Jan 24 '14 at 15:33
21

Press Ctrl + F5 to run the program instead of F5.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
waqasahmed
  • 3,555
  • 6
  • 32
  • 52
  • 2
    Ctrl + F5 starts the application without debugging to you wont see anything in the output window. – Richard Adnams Mar 15 '11 at 16:53
  • 6
    This worked for me; I just wanted to see what the program was outputting, none of the console output was showing up. This caused it to keep the console window open after running so I could see the output. – davenpcj Apr 16 '13 at 00:37
  • +1, this is the one, if you don't actually want to debug (shows the console, and allows to read the output). – mlvljr Apr 13 '14 at 19:53
8

System.Diagnostics.Debug.WriteLine() will work, but you have to be looking in the right place for the output. In Visual Studio 2010, on the menu bar, click Debug -> Windows -> Output. Now, at the bottom of the screen docked next to your error list, there should be an output tab. Click it and double check it's showing output from the debug stream on the dropdown list.

P.S.: I think the output window shows on a fresh install, but I can't remember. If it doesn't, or if you closed it by accident, follow these instructions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lordcheeto
  • 1,092
  • 12
  • 16
0

To keep open your windows console and to not use other output methods rather than the standard output stream cout go to Name-of-your-project -> Properties -> Linker -> System.

Once there, select the SubSytem Tab and mark Console (/SUBSYSTEM:CONSOLE). Once you have done this, whenever you want to compile use Ctrl + F5 (Start without debugging) and your console will keep opened. :)

Alex
  • 975
  • 10
  • 24
0

I run into this frequently for some reason, and I can't fathom why this solution hasn't been mentioned:

Click ViewOutput (or just hold Ctrl and hit W > O)

Console output then appears where your Error List, Locals, and Watch windows are.

Note: I'm using Visual Studio 2015.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Methodician
  • 2,396
  • 5
  • 30
  • 49
  • The Output window you mention looks like it is generated by the debugger since it has lots of lines relating to assemblies being loaded, threads starting and stopping etcetera. This is different than console output. – PeterVermont May 10 '16 at 20:37
0

Visual Studio is by itself covering the console window, try minimizing Visual Studio window they are drawn over each other.

Niklas E.
  • 1,848
  • 4
  • 13
  • 25
-1

In Program.cs, between:

static int Main(string[] agrs)
{

and the rest of your code, add:

#if DEBUG
    int rtn = Main2(args);
    Console.WriteLine("return " + rtn);
    Console.WriteLine("ENTER to continue.");
    Console.Read();
    return rtn;
}

static int Main2(string[] args)
{
#endif
George
  • 1
-4

You could create 2 small methods, one that can be called at the beginning of the program, the other at the end. You could also use Console.Read(), so that the program doesn't close after the last write line.

This way you can determine when your functionality gets executed and also when the program exists.

startProgram()
{
     Console.WriteLine("-------Program starts--------");
     Console.Read();
}


endProgram()
{
    Console.WriteLine("-------Program Ends--------");
    Console.Read();
}
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
  • I don't see how this answer brings new information to the question since the Console.Read() possibility has already been stated before. – ForceMagic Oct 10 '12 at 18:29