I'm writing a fairly simple console application and I'm wanting to set the console window to a specific size. I've been trying both Console.SetWindowSize() and Console.WindowHeight/WindowWidth, but neither seem to change the actual window size. From experimentation what it is they appear to do is change the area within the window that text can be drawn, which wasn't quite what I was expecting.
Furthermore, it seems that Console.Clear() doesn't quite work, either. The result seems to be unpredictable. Sometimes previous output is cleared, sometimes not and new output is just written over the top (with the old occasionally visible).
I've noticed that if I "mess around" with the window size manually by dragging it around with my mouse, the output becomes quite garbled. It looks like current and old data overwrites each other, more or less at random.
public TestScreen()
{
Console.WindowHeight = 20;
Console.WindowWidth = 20;
Console.WriteLine("Test 1");
Console.WriteLine("Max height: " + Console.LargestWindowHeight.ToString());
Console.WriteLine("Max width: " + Console.LargestWindowWidth.ToString());
Console.ReadKey();
Console.Clear();
Console.SetWindowSize(10, 10);
Console.WriteLine("Test 2");
Console.WriteLine("Max height: " + Console.LargestWindowHeight.ToString());
Console.WriteLine("Max width: " + Console.LargestWindowWidth.ToString());
Console.ReadKey();
Console.Clear();
}
Results: 1 , 2 (after pressing any key), 3 (after playing around with the window size)
I found the solution on a Reddit thread. The problem was the new style Console window included in Windows 11 (that was a critical bit of information that I didn't know was important).
As per the original solution I fixed it by changing the output to Windows Console Host. This can be done in Settings > Privacy > For Developers > Terminal.