Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the size of the console window in C#?

I'm creating a prototype for a piece of functionality that I will be implementing in an application, specifically I am displaying data from a collection to the console window but unfortunately some of the lines span wider than the default width of the console window.

alt text

I've done a bit of digging and found that the only way to increase the width of the window is to dig into the Kernel32.dll and do it manually.

As elegant as pInvoke is, does a "shorter" way exist to increase the width e.g. (Console.Width = 300) or is using the Kernel32.dll the only way.

Thanks!

like image 378
Jamie Keeling Avatar asked Nov 08 '10 10:11

Jamie Keeling


1 Answers

No programming is required. Create a shortcut to your program on the desktop. Right-click it, Properties, Layout tab. Adjust the Width property of the screen buffer and window size.

But you can do it programmatically too, those Windows API functions you found are wrapped by the Console class as well. For example:

    static void Main(string[] args) {
        Console.BufferWidth = 100;
        Console.SetWindowSize(Console.BufferWidth, 25);
        Console.ReadLine();
    }
like image 111
Hans Passant Avatar answered Oct 14 '22 20:10

Hans Passant