Console.WindowWidth 属性

定义

获取或设置控制台窗口的宽度。

public static int WindowWidth { [System.Runtime.Versioning.UnsupportedOSPlatform("android")] [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
public static int WindowWidth { [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static int WindowWidth { get; set; }
public static int WindowWidth { get; set; }

属性值

控制台窗口的宽度,以列为单位。

属性

例外

属性 WindowWidth 的值或属性 WindowHeight 的值小于或等于零。

属性的值 WindowHeight 加上 属性的值 WindowTop 大于或等于 Int16.MaxValue

WindowWidth 属性的值或 WindowHeight 属性的值大于当前屏幕分辨率和控制台字体的最大可能的窗口宽度或高度。

读取或写入信息时发生错误。

设置操作在 Windows 之外的操作系统上调用。

示例

此示例演示 SetWindowSize 了 方法以及 WindowWidthWindowHeight 属性。 必须运行此示例才能看到更改控制台窗口大小的完整效果。

该示例报告设置为 85 列和 43 行的控制台窗口的尺寸,然后等待按键。 按下任意键时,控制台窗口的尺寸减半,报告新维度,示例将等待另一次按键。 最后,当按下任何键时,控制台窗口将还原到其原始尺寸,并且示例终止。

// 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.

*/

注解

尝试在重定向输出时设置 属性的值 WindowWidth 会引发 ArgumentOutOfRangeExceptionIOException 异常。 为防止异常,仅当属性返回 falseIsOutputRedirected,才能设置此属性的值。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1