Console.OutputEncoding 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置控制台用于写入输出的编码。
public:
static property System::Text::Encoding ^ OutputEncoding { System::Text::Encoding ^ get(); void set(System::Text::Encoding ^ value); };
public static System.Text.Encoding OutputEncoding { get; [System.Runtime.Versioning.UnsupportedOSPlatform("android")] [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] set; }
public static System.Text.Encoding OutputEncoding { get; set; }
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member OutputEncoding : System.Text.Encoding with get, set
static member OutputEncoding : System.Text.Encoding with get, set
Public Shared Property OutputEncoding As Encoding
属性值
用于写入控制台输出的编码。
- 属性
例外
设置操作中的属性值为 null.
执行此操作期间发生错误。
应用程序无权执行此操作。
注解
控制台使用输出编码将应用程序编写的字符转换为相应的控制台显示字符。 控制台使用的默认代码页由系统区域设置确定。
从 .NET Framework 4 开始,属性获取操作可能会返回缓存的值,而不是控制台的当前输出编码。 如果属性的值 OutputEncoding 不是对属性的赋值 OutputEncoding (例如调用 Windows SetConsoleOutputCP 函数)修改的,则可能会出现这种情况。
调用方说明
在 Unicode 编码中, Console 类支持使用 UTF8Encoding 类进行 UTF-8 编码,从 .NET Framework 4.5 开始,它支持类的 UnicodeEncoding UTF-16 编码。 不支持使用类的 UTF32Encoding UTF-32 编码。 尝试将输出编码设置为 UTF-32 将引发一个 IOException。
请注意,成功向控制台显示 Unicode 字符需要满足以下条件:
主机必须使用 TrueType 字体(如 Lucida Console 或 Consolas)来显示字符。
主机使用的字体必须定义要显示的特定字形或字形。 如果基字体不包含该字形的定义,控制台可以利用字体链接来显示链接字体的字形。
有关主机对 Unicode 编码的支持的详细信息,请参阅类中的 Console “控制台的 Unicode 支持”部分。