C
C#2y ago
Cience

✅ console.writeline not working on Winforms application

I attempted to use this post to help me https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application use a console on a WinForms application but the output will not show, I have read over the "Duplicate questions" page and they all don't answer my question I've tried using Debug. WriteLine but it doesn't work either. I'm assuming I'm missing a setting of some sort but they all say "use Managed Compatibility Mode" but its been gone for years, I'm having it open the console on a button press, and it opens the console it just won't output anything I've also tried "FreeConsole()" but it doesn't even open.
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

private void button1_Click(object sender, EventArgs e)
{
AllocConsole();
string message1 = "test123";
Console.WriteLine(message1);
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

private void button1_Click(object sender, EventArgs e)
{
AllocConsole();
string message1 = "test123";
Console.WriteLine(message1);
}
Stack Overflow
How do I show a console output/window in a forms application?
To get stuck in straight away, a very basic example: using System; using System.Windows.Forms; class test { static void Main() { Console.WriteLine("test"); MessageBox.Sh...
46 Replies
Cience
CienceOP2y ago
This is Visual studio 2022 C# winforms
Cience
CienceOP2y ago
i just relized that the output is going to this part under my workspace
Cience
CienceOP2y ago
the debug output console how could i fix it?
tacosontitan
tacosontitan2y ago
Why are you trying to write to the console in a windows forms applications anyways? The Debug.WriteLine invocations would write to this.
Cience
CienceOP2y ago
Console.Writeline does the same thing
tacosontitan
tacosontitan2y ago
That's fine So what's the issue then?
Cience
CienceOP2y ago
tacosontitan
tacosontitan2y ago
Are you just trying to open a dedicated console window from your form?
Cience
CienceOP2y ago
look here is the code right uh i think thats how to explain it
Cience
CienceOP2y ago
i want the text to show up on the console
Cience
CienceOP2y ago
but it doesnt
tacosontitan
tacosontitan2y ago
Well, move the AllocConsole invocation to your form load method as the post describes for starters, otherwise you're allocating a console every time you click the button. That could be part of the problem
Cience
CienceOP2y ago
yea doesnt work
Cience
CienceOP2y ago
and i want it to open on a button press anyway
tacosontitan
tacosontitan2y ago
Then add some logic to determine if it was already opened so you don't keep reallocating:
private bool _consoleAllocated = false;
private void button1_Click(object sender, EventArgs e) {
if (!_consoleAllocated) {
AllocConsole();
_consoleAllocated = true;
}

string meessage1 = "test123";
Console.WriteLine(message1);
}
private bool _consoleAllocated = false;
private void button1_Click(object sender, EventArgs e) {
if (!_consoleAllocated) {
AllocConsole();
_consoleAllocated = true;
}

string meessage1 = "test123";
Console.WriteLine(message1);
}
That's a good start in trying to isolate the problem
Cience
CienceOP2y ago
it just opens like normal when i hit the button
tacosontitan
tacosontitan2y ago
Share your full code file When I do exactly as the SO answer describes it works for me just fine
tacosontitan
tacosontitan2y ago
Cience
CienceOP2y ago
this is all i ahve
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;

namespace UI
{
public partial class UI: Form
{
public UI()
{
InitializeComponent();
}

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

private bool _consoleAllocated = false;

private void button1_Click(object sender, EventArgs e)
{
if (!_consoleAllocated)
{
AllocConsole();
_consoleAllocated = true;
}
Console.Title = "Title";
string meessage1 = "test123";
Console.WriteLine(meessage1);
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;

namespace UI
{
public partial class UI: Form
{
public UI()
{
InitializeComponent();
}

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

private bool _consoleAllocated = false;

private void button1_Click(object sender, EventArgs e)
{
if (!_consoleAllocated)
{
AllocConsole();
_consoleAllocated = true;
}
Console.Title = "Title";
string meessage1 = "test123";
Console.WriteLine(meessage1);
}
}
@tacosontitan am i missing something
tacosontitan
tacosontitan2y ago
Yeah, you're not defining _consoleAllocated anywhere.
Cience
CienceOP2y ago
oh mb that was above button1 i ahve it
tacosontitan
tacosontitan2y ago
Are you excluding code?
Cience
CienceOP2y ago
not on purpose thats my fault this is just a test application
tacosontitan
tacosontitan2y ago
What version of .NET are you targeting?
Cience
CienceOP2y ago
Cience
CienceOP2y ago
should i be using more upto date
tacosontitan
tacosontitan2y ago
Is there a reason you're targeting that specific version? IMHO, absolutely unless you have a solid reason not to
Cience
CienceOP2y ago
not really its just what i made it with my harddrive corrupted a few days ago so my settings are off im using a old one
tacosontitan
tacosontitan2y ago
I would target .NET 6 or 7 at a minimum
Cience
CienceOP2y ago
ok what framework are you on when you made the test
tacosontitan
tacosontitan2y ago
When I created the project it defaulted to net6.0-windows.
Cience
CienceOP2y ago
hm ok how come the framework isnt showing up
Cience
CienceOP2y ago
i installed it
Cience
CienceOP2y ago
7.0 ill just google it
tacosontitan
tacosontitan2y ago
You can't directly upgrade from .NET Framework to .NET or .NET Core You can either upgrade it by hand, with an upgrade tool, or just recreate the project with a modern template. I prefer the by hand if it's a small project that I just chose the wrong template for
Cience
CienceOP2y ago
how do i upgrade it byhand could you tell me a upgrade tool
tacosontitan
tacosontitan2y ago
I can drop a link for the upgrade tool in a moment, but you said it's just a basic test project right? So you're just getting started?
Cience
CienceOP2y ago
yea but for the future yea is it
tacosontitan
tacosontitan2y ago
No added packages or dependencies?
Cience
CienceOP2y ago
not currently
tacosontitan
tacosontitan2y ago
Right click the project -> Unload Project Right click the project -> Edit Copy contents to notepad for sanity check Replace with:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<LanguageVersion>latest</LanguageVersion>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<LanguageVersion>latest</LanguageVersion>
</PropertyGroup>
</Project>
Save. Right click the project -> reload I can drop the upgrade tool if you're not comfortable with that approach
Cience
CienceOP2y ago
oh alright ill try it
tacosontitan
tacosontitan2y ago
The upgrade tool takes longer IMHO Its a better fit for an established project.
Cience
CienceOP2y ago
I upgraded my project to 6.0 and it worked thanks @tacosontitan
tacosontitan
tacosontitan2y ago
No problem

Did you find this page helpful?