Home C# Console Methods SetWindowPosition() C# Console SetWindowPosition() Method Definition In C#, Console.SetWindowPosition sets the origin of the visible window inside the larger screen buffer—similar to scrolling which region you see.
Arguments are the left and top of that origin. Values must keep the whole window inside the buffer. Like other window APIs, unsupported hosts may throw.
This is a static member of System.Console. You call it with the type name, such as Console.SetWindowPosition(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax Pass the buffer coordinates for the window’s upper-left.
Console.SetWindowPosition(0, 0);
Console.SetWindowPosition(0, 10); Call What it does Console.SetWindowPosition(left, top)Sets the visible window origin in the buffer.
This is not the same as moving the OS window on the desktop; it selects the visible slice of the buffer.
Library declarations public static void SetWindowPosition(int left, int top); Parameters Parameter Description left Buffer column for the window origin. top Buffer row for the window origin.
Return Value Return Value Description void Visible region updates when supported.
C# Examples 1. Origin zero 2. Scroll down 3. Report origin Example 1 — Reset Window Origin to 0,0 Move the visible area back to the top of the buffer.
using System;
class Program {
static void Main() {
try {
Console.SetWindowPosition(0, 0);
Console.WriteLine("WindowTop=" + Console.WindowTop);
Console.WriteLine("WindowLeft=" + Console.WindowLeft);
} catch (Exception ex) {
Console.WriteLine("Not supported: " + ex.GetType().Name);
}
}
} How It Works Origin home. (0,0) shows the top-left of the buffer.Confirm properties. WindowTop/Left reflect the origin.Try/catch. Keeps demos safe in IDEs.Sample success. Both values report 0.Example 2 — Shift the Visible Region Down If the buffer is taller than the window, move the origin down.
using System;
class Program {
static void Main() {
try {
int maxTop = Math.Max(0, Console.BufferHeight - Console.WindowHeight);
int top = Math.Min(5, maxTop);
Console.SetWindowPosition(0, top);
Console.WriteLine("WindowTop=" + Console.WindowTop);
} catch (Exception) {
Console.WriteLine("SetWindowPosition unavailable");
}
}
} How It Works Max top. Keep the window fully inside the buffer.Clamp. Use at most 5, or less if the buffer is short.Effect. Visible text region scrolls down.Fallback. Unsupported hosts print the unavailable message.Example 3 — Print WindowLeft and WindowTop Inspect the current origin without changing it.
using System;
class Program {
static void Main() {
Console.WriteLine("WindowLeft=" + Console.WindowLeft);
Console.WriteLine("WindowTop=" + Console.WindowTop);
}
} How It Works Read-only demo. Safe everywhere.Typical zeros. Fresh consoles often start at the origin.Before scrolling. Check values prior to SetWindowPosition.Pairs with size. Origin + window size must fit the buffer.Frequently Asked Questions Does this move the OS window on screen? No. It selects which part of the character buffer is visible.
What happens with bad coordinates? An exception is thrown if the window would leave the buffer.
When is it useful? When the buffer is larger than the window and you want to scroll programmatically.
Related methods? SetWindowSize and SetBufferSize.
Will it work in every terminal? No. Wrap in try/catch for hosts that lack a classic console window.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion