0

Anyone have any ideas why Console.Write(string) no longer works in a Visual Studio 2017 C# Windows Forms application?

Here's some sample code to reproduce:

Console.WriteLine("Results: ");
const int count = 15;
for (int i = 0; i < count; i++)
{
    if (i == (count - 1))
        Console.Write(i);
    else
        Console.Write(i + ", ");
}

I've already submitted a bug to the VS team at Microsoft and they've been less than helpful. They don't seem to understand the issue for some reason? I don't know how to be any more clear than this right here.

Bug report: https://developercommunity2.visualstudio.com/t/consolewritestring-does-not-work-anymore/1241476

  • So, you are not seeing this printed on the console, right? – Jamshaid K. Nov 09 '20 at 15:41
  • That's correct. – Aaron Chapman Nov 09 '20 at 15:42
  • When did it work before? You mention in that bug report that you expect this to print to a console, but WinForms by default doesn't create a console window. – Joe Sewell Nov 09 '20 at 15:43
  • Are you saying this has *ever* worked? [Questions we have suggest otherwise](https://stackoverflow.com/q/5706723/4137916); if `AllocConsole` isn't called (and WinForms doesn't normally do this) you're not getting a console. – Jeroen Mostert Nov 09 '20 at 15:43
  • 1
    You mean Visual Studio's Output Panel, of course (?). Or Immediate Window, depending on your configuration. If that's the case, this happened a while ago and went on for some time. It has been solved (or, better, the default behavior has been restored) many versions before the current. – Jimi Nov 09 '20 at 15:45
  • @JoeSewell In the past, it's always showed up in the 'Debug' Console window at the bottom of VS whenever you run your application. Exactly the same way "Console.WriteLine" shows up. I don't know exactly when it stopped working, but it happened some time in the past year or so. I don't use it often, but it's so much easier than building the string with StringBuilder and then using Console.WriteLine when attempting to debug something quickly. It's fundamentally the same as Console.WriteLine only without the proceeding line terminator. There's no reason one should work but not the other. – Aaron Chapman Nov 09 '20 at 15:47
  • @JeroenMostert In my post above, "no longer works" implies that it did indeed work in the past, yes. – Aaron Chapman Nov 09 '20 at 15:49
  • Oh, I see. I thought, when reading your bug report, that you somehow wanted a production app to write to a console window. You're just saying you want a production WinForms app to write to the VS Output window when a debugger is attached? And that WriteLine *does* work for this purpose, but Write doesn't? It sounds like maybe the behavior of that window changed in 2017 to only show text line-by-line instead of character-by-character. – Joe Sewell Nov 09 '20 at 15:50
  • Well -- as a simple optimization I can well imagine the team implementing buffering. What happens in the output window for a forms app was never a contract to begin with, so that's a "bug" that may well be called a feature as well (especially if it speeds up things other than a WinForms app doing some debug logging). You could try `Console.OpenStandardOutput().Flush()`, but there's no real guarantee that works or would continue to work either. It would not seem particularly burdensome to simply call `.WriteLine()` at select places. Alternatively, use tracepoints in the debugger. – Jeroen Mostert Nov 09 '20 at 15:53
  • @JoeSewell Yes that is correct, I'm simply debugging a WinForms app and typically, in most cases, Console.WriteLine works for my needs. However, every once in a blue moon, I need to use it in a loop and (in the past) I've always used Console.Write due to it being so simple and quick. It's no different from Console.WriteLine other than, it doesn't output a line terminator at the end. – Aaron Chapman Nov 09 '20 at 15:58
  • @JeroenMostert I could understand your thoughts possibly explaining why they did what they did, if it is in fact, 'a feature' they intended on changing. But if that's the case, they should've let the community know: "Console.WriteLine" will continue to work as usual, however, "Console.Write" will not work as expected from this point forward in a WinForms app. With that being said, it still doesn't make much sense to me when all that is different is a proceeding line terminator... – Aaron Chapman Nov 09 '20 at 16:04
  • 2
    A quick test shows that `System.Diagnostics.Debug` does not suffer from the same issue (in VS 2019, at least) and is largely interchangeable save for (curiously) lacking a `.WriteLine()` with no parameters (but `WriteLine("")` would then do). Arguably using that is clearer than `Console` in a forms app as well, as that could conceivably be used for *actual*, end user readable output (even though most Forms apps don't allocate a console). As a bonus you get `Indent`/`Unindent`, I guess? – Jeroen Mostert Nov 09 '20 at 16:07
  • @AaronChapman what you consider a bug is a side-effect. The application itself writes to the console just fine. You refer to *reading the output in VS while debugging only*. That's not a common scenario. Applications normally write to logs, not the console, when deployed. When debugging, almost all programmers use `Debug.WriteLine` or `Trace.WriteLine` when they feel too lazy to use proper logging. Not `Console`. It's not just a line delimiter - consoles are line oriented and a debugger may well decide that a message is completed only when a line completes – Panagiotis Kanavos Nov 09 '20 at 16:09
  • @AaronChapman if what you tried was common, a lot of people would have noticed a long time ago. Instead only a few people did – Panagiotis Kanavos Nov 09 '20 at 16:10
  • @JeroenMostert Yes, I see. I was hoping I would not need to include another namespace in my project to simply output to the debug window in a loop. However, it seems that will be my only option going forward. – Aaron Chapman Nov 09 '20 at 16:20
  • 1
    @AaronChapman you don't need to add anything, the `Diagnostics` namespace is always available. This is well documented by the way, as `Debug` and `Trace` were the common way of logging in the first decade of .NET. People moved to better/easier logging libraries like log4net since 2010 – Panagiotis Kanavos Nov 09 '20 at 16:22
  • @PanagiotisKanavos Yes, clearly it is not a common scenario and I'm well aware of writing to logs for debugging purposes. I only use Console.Write/WriteLine functions for simplicity's sake when checking my results quickly as I go. If it was even common, for me to use these functions, I suppose I would've noticed this a long time ago. – Aaron Chapman Nov 09 '20 at 16:24

3 Answers3

2

It is not going to work as a winforms project will not create console window in here. If you want to see it the console output, you need to open integrated Output window of visual studio.

Step 1
Click on View in the navigation menu of Visual studio.

Step 2
Then click Output or you can use the shortcut Ctrl+W, O to open the window for you.

View Menu

Step 3

Now, replace all the Console.WriteLines(...); with Debug.WriteLines(...); which will put all the lines that you want to output.

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
  • I'm hesitant to down-vote your answer because I'm under the impression you think I'm too uneducated to know that I need to open the output window before debugging my project, but I am considering... The output window, by default, opens EVERY TIME you run your application in 'Debug' mode. But thanks for your input. – Aaron Chapman Nov 09 '20 at 16:09
  • 2
    @AaronChapman if you downvoted you'd be very wrong. `Console` is *not* used for logging, `Debug` and `Trace` are. They don't just write to the "console", they write to .NET's logging and tracing infrastructure. By default, `Debug` writes to the *debug output* which every debugger captures. This is a good answer – Panagiotis Kanavos Nov 09 '20 at 16:17
  • 1
    @AaronChapman I'm sure you know how to open it, but whenever you write an answer you consider people who are going to come in future to see what has someone answered and they will see that the answer is detailed or not it will make life easier for others as well. Just don't take offense of something which is explaining something to you. – Jamshaid K. Nov 09 '20 at 16:18
  • And sometimes, the output window is accidently closed by people, do you think it will still open up by default? It won't open up. My output window doesn't open up by default. Have never modified the default settings as well. – Jamshaid K. Nov 09 '20 at 16:26
  • @PanagiotisKanavos I wouldn't be wrong at all, I never said `Console` was 'intended' to be used for logging, and I'm well aware that isn't the norm. However, like I said in a previous comment, I only use it for quick checking and only in rare cases. I'm not here to argue about 'best practices' with you. Only wondering why it suddenly stopped working as it has in the past. Obviously I just need to swap `Console` for `Debug` from now on. There has not been a good explanation for why `Console.Write` has stopped working now, but an alternative was suggested to me, and I will use it. – Aaron Chapman Nov 09 '20 at 16:31
0

Microsoft has changed the functionality of their product, and they've done so without many people even noticing because it's such a niche case scenario... Just as @Jimi stated in his comment above.

You mean Visual Studio's Output Panel, of course (?). Or Immediate Window, depending on your configuration. If that's the case, this happened a while ago and went on for some time. It has been solved (or, better, the default behavior has been restored) many versions before the current.

E_net4
  • 27,810
  • 13
  • 101
  • 139
-1

I followed your Bug Report and reproduced your issue.

The root cause is: your project is not a console app, i.e. a console application will instantly display the Console.Write output. However, in WinForms, Visual Studio reads the console output but doesn’t print it to the output pane until a newline character is fed to it. In general, windows forms apps don’t use Console.Write[Line] as it doesn’t go anywhere when you run outside of visual studio.

If you insist using Console.Write, add a newline character like this: enter image description here Console.Write has outputs!

You can also follow Jamshaid Kamran's advice to open View-Output and change Console to Debug, or there are solutions here to open Console window but are very inconvenient. In my experience, I would prefer to use MessageBox.Show("Results") to view something temporarily.

Mia Wu-MSFT
  • 182
  • 6