In C#, Console.SetWindowSize changes the visible console window’s width and height in character cells.
The window must fit inside the current screen buffer. If you need a larger window, increase the buffer first with SetBufferSize. Unsupported or redirected consoles may throw.
This is a static member of System.Console. You call it with the type name, such as Console.SetWindowSize(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Pass visible width and height; ensure the buffer is large enough.
using System;
class Program {
static void Main() {
try {
int w = Math.Max(Console.BufferWidth, 100);
int h = Math.Max(Console.BufferHeight, 40);
Console.SetBufferSize(w, h);
Console.SetWindowSize(100, 30);
Console.WriteLine("OK");
} catch (Exception) {
Console.WriteLine("Resize not available");
}
}
}
Output
OK
How It Works
Buffer first. Makes room for the larger window.
Then window. SetWindowSize(100, 30) can succeed.
Failure path. Prints a short message when unsupported.
Interactive consoles. Desktop terminals are the usual target.
Example 3 — Show Current Window Size
Read sizes without changing them.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("WindowWidth=" + Console.WindowWidth);
Console.WriteLine("WindowHeight=" + Console.WindowHeight);
}
}
Output
WindowWidth=120
WindowHeight=30
How It Works
No resize. Safe in every host.
Sample values. Numbers vary by terminal.
Planning. Read before calling SetWindowSize.
Pairs with buffer. Compare to BufferWidth/Height.
Frequently Asked Questions
Window is what you see; buffer can be larger and scrollable.
Size larger than buffer, or host without a resizable console.
Yes when growing the window beyond the current buffer.
Often no. Wrap calls in try/catch for portability.