Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console.WindowWidth Property

Gets or sets the width of the console window.

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

public static int WindowWidth { get; set; }
/** @property */
public static int get_WindowWidth ()

/** @property */
public static void set_WindowWidth (int value)

public static function get WindowWidth () : int

public static function set WindowWidth (value : int)

Not applicable.

Property Value

The width of the console window measured in columns.

Exception typeCondition

ArgumentOutOfRangeException

The value of the WindowWidth property or the value of the WindowHeight property is less than or equal to 0.

-or-

The value of the WindowHeight property plus the value of the WindowTop property is greater than or equal to Int16.MaxValue.

-or-

The value of the WindowWidth property or the value of the WindowHeight property is greater than the largest possible window width or height for the current screen resolution and console font.

IOException

Error reading or writing information.

This example demonstrates the SetWindowSize method, and the WindowWidth and WindowHeight properties. You must run the example to see the full effect of changing the console window size.

The example reports the dimensions of a console window set to 85 columns and 43 rows, then waits for a key press. When any key is pressed, the dimensions of the console window are halved, the new dimensions are reported, and the example waits for another key press. Finally, when any key is pressed the console window is restored to its original dimensions and the example terminates.

// This example demonstrates the Console.SetWindowSize method,
//                           the Console.WindowWidth property, 
//                       and the Console.WindowHeight property.
using System;

class Sample 
{
    public static void Main() 
    {
    int origWidth, width;  
    int origHeight, height;
    string m1 = "The current window width is {0}, and the " +
                "current window height is {1}.";
    string m2 = "The new window width is {0}, and the new " +
                "window height is {1}.";
    string m4 = "  (Press any key to continue...)";
//
// Step 1: Get the current window dimensions.
//
    origWidth  = Console.WindowWidth;
    origHeight = Console.WindowHeight;
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true); 
//
// Step 2: Cut the window to 1/4 its original size.
//
    width  = origWidth/2;
    height = origHeight/2;
    Console.SetWindowSize(width, height);
    Console.WriteLine(m2, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true); 
//
// Step 3: Restore the window to its original size.
//
    Console.SetWindowSize(origWidth, origHeight);
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    }
}
/*
This example produces the following results:

The current window width is 85, and the current window height is 43.
  (Press any key to continue...)
The new window width is 42, and the new window height is 21.
  (Press any key to continue...)
The current window width is 85, and the current window height is 43.

*/

// This example demonstrates the Console.SetWindowSize method,
//                           the Console.WindowWidth property, 
//                       and the Console.WindowHeight property.
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        int origWidth, width;
        int origHeight, height;
        String m1 = "The current window width is {0}, and the " 
            + "current window height is {1}.";
        String m2 = "The new window width is {0}, and the new " 
            + "window height is {1}.";
        String m4 = "  (Press any key to continue...)";
        //
        // Step 1: Get the current window dimensions.
        //
        origWidth = Console.get_WindowWidth();
        origHeight = Console.get_WindowHeight();
        Console.WriteLine(m1, System.Convert.ToString(Console.get_WindowWidth()),
            System.Convert.ToString(Console.get_WindowHeight()));
        Console.WriteLine(m4);
        Console.ReadKey(true);
        //
        // Step 2: Cut the window to 1/4 its original size.
        //
        width = origWidth / 2;
        height = origHeight / 2;
        Console.SetWindowSize(width, height);
        Console.WriteLine(System.Convert.ToString(m2), 
            System.Convert.ToString(Console.get_WindowWidth()),
            System.Convert.ToString(Console.get_WindowHeight()));
        Console.WriteLine(m4);
        Console.ReadKey(true);
        //
        // Step 3: Restore the window to its original size.
        //
        Console.SetWindowSize(origWidth, origHeight);
        Console.WriteLine(System.Convert.ToString(m1),
            System.Convert.ToString(Console.get_WindowWidth()), 
            System.Convert.ToString(Console.get_WindowHeight()));
    } //main
} //Sample
/*
This example produces the following results:

The current window width is 85, and the current window height is 43.
  (Press any key to continue...)
The new window width is 42, and the new window height is 21.
  (Press any key to continue...)
The current window width is 85, and the current window height is 43.

*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft