This documentation is archived and is not being maintained.

Console.SetWindowPosition Method

Sets the position of the console window relative to the screen buffer.

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

public static void SetWindowPosition(
	int left,
	int top


Type: System.Int32
The column position of the upper left corner of the console window.
Type: System.Int32
The row position of the upper left corner of the console window.


left or top is less than zero.


left + WindowWidth is greater than BufferWidth.


top + WindowHeight is greater than BufferHeight.


The user does not have permission to perform this action.


An I/O error occurred.

The operating system window displays the console window, and the console window displays a portion of the screen buffer. The SetWindowPosition method affects the position of the console window relative to the screen buffer, but does not affect the position of the operating system window relative to the desktop.

The console and operating system windows generally do not affect each other. However, if the screen buffer cannot be displayed in the current boundaries of the console window, the operating system automatically appends scroll bars to the operating system window. In that case, moving the operating system window scroll bars affects the position of the console window, and moving the console window with the SetWindowPosition method affects the position of the operating system window scroll bars.

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

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.