Console.SetWindowSize() Method in C#

Console.SetWindowSize(Int32, Int32) Method is used to change the height and width of the console window to the specified values.

Syntax: public static void SetWindowSize (int width, int height);

Parameters:
width: The width of the console window measured in columns.
height: The height of the console window measured in rows.



Exceptions:

  • ArgumentOutOfRangeException:
    • If the width or height is less than or equal to zero
    • width plus WindowLeft or height plus WindowTop is greater than or equal to MaxValue
    • width or height is greater than the largest possible window width or height for the current screen resolution and console font.
  • IOException: If an I/O error occured.

Example 1: Getting the current dimensions of the window.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to get the current
// window width and Height
using System;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
        Console.WriteLine(Console.WindowWidth);
        Console.WriteLine(Console.WindowHeight);
    }
}
}

chevron_right


Output:

Example 2: Setting the value of SetWindowSize

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Console.SetWindowSize Property
using System;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
        // Passed 40, 40 to SetWindowSize to 
        // change window size to 40 by 40
        Console.SetWindowSize(40, 40);.
  
        // Printing the current dimensions
        Console.WriteLine(Console.WindowWidth);
        Console.WriteLine(Console.WindowHeight);
    }
}
}

chevron_right


Output:

Note: See the Horizontal Scroll Bar at the bottom of the Window in both the images.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.