Console.WindowTop Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the top position of the console window area relative to the screen buffer.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static int WindowTop { get; set; }

Property Value

Type: System.Int32

The uppermost console window position measured in rows.

Exception Condition

In a set operation, the value to be assigned is less than zero.


As a result of the assignment, WindowTop plus WindowHeight would exceed BufferHeight.


Error reading or writing information.

The console represents a rectangular window into a larger rectangular buffer area. Both the window and the buffer are measured vertically by their number of rows and horizontally by their number of columns. The dimensions of the buffer area are defined by the BufferHeight and BufferWidth properties. The dimensions of the console area are defined by the WindowHeight and WindowWidth properties. The WindowTop property determines which row of the buffer area is displayed in the first column of the console window. The value of the WindowTop property can range from 0 to BufferHeight - WindowHeight. Attempting to set it to a value outside that range throws an ArgumentOutOfRangeException.

Attempting to set the value of the WindowTop property when output is redirected throws an IOException exception. To prevent the exception, you can set the value of this property only if the IsOutputRedirected property returns false.

The following example demonstrates the WindowLeft, WindowTop, WindowWidth, WindowHeight, BufferWidth, BufferHeight, and CursorVisible properties; and the SetWindowPosition, SetBufferSize, and ReadKey methods. The example draws a grid pattern in the screen buffer based on the screen buffer width. Then the example moves the console window in response to which of the UP ARROW, DOWN ARROW, LEFT ARROW, or RIGHT ARROW console keys is pressed. The grid pattern helps you see the movement of the console window relative to the screen buffer.

// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
using System;
using System.Text;
using System.IO;
class Sample 
    public static int saveBufferWidth;
    public static int saveBufferHeight;
    public static int saveWindowHeight;
    public static int saveWindowWidth;
    public static bool saveCursorVisible;
    public static void Main() 
    string m1 = "1) Press the cursor keys to move the console window.\n" +
                "2) Press any key to begin. When you're finished...\n" +
                "3) Press the Escape key to quit.";
    string g1 = "+----";
    string g2 = "|    ";
    string grid1;
    string grid2;
    StringBuilder sbG1 = new StringBuilder();
    StringBuilder sbG2 = new StringBuilder();
    ConsoleKeyInfo cki;
    int y;
    saveBufferWidth  = Console.BufferWidth;
    saveBufferHeight = Console.BufferHeight;
    saveWindowHeight = Console.WindowHeight;
    saveWindowWidth  = Console.WindowWidth;
    saveCursorVisible = Console.CursorVisible;

// Set the smallest possible window size before setting the buffer size.
    Console.SetWindowSize(1, 1);
    Console.SetBufferSize(80, 80);
    Console.SetWindowSize(40, 20);

// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
    for (y = 0; y < Console.BufferWidth/g1.Length; y++)
    sbG1.Append(g1, 0, Console.BufferWidth%g1.Length);
    sbG2.Append(g2, 0, Console.BufferWidth%g2.Length);
    grid1 = sbG1.ToString();
    grid2 = sbG2.ToString();

    Console.CursorVisible = false;
    for (y = 0; y < Console.BufferHeight-1; y++)
        if (y%3 == 0)

    Console.SetWindowPosition(0, 0);
        cki = Console.ReadKey(true);
        switch (cki.Key) 
            case ConsoleKey.LeftArrow:
                if (Console.WindowLeft > 0) 
                            Console.WindowLeft-1, Console.WindowTop);
            case ConsoleKey.UpArrow:
                if (Console.WindowTop > 0) 
                            Console.WindowLeft, Console.WindowTop-1);
            case ConsoleKey.RightArrow:
                if (Console.WindowLeft < (Console.BufferWidth-Console.WindowWidth)) 
                            Console.WindowLeft+1, Console.WindowTop);
            case ConsoleKey.DownArrow:
                if (Console.WindowTop < (Console.BufferHeight-Console.WindowHeight)) 
                            Console.WindowLeft, Console.WindowTop+1);
    while (cki.Key != ConsoleKey.Escape);  // end do-while
    } // end try
    catch (IOException e) 
        Console.SetWindowSize(1, 1);
        Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
        Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
        Console.CursorVisible = saveCursorVisible;
    } // end Main
} // end Sample
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.


|    |    |    |
|    |    |    |
|    |    |    |
|    |    |    |


.NET Framework
Available since 2.0
Return to top