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; set; }
public static System.Text.Encoding OutputEncoding { get; [System.Runtime.Versioning.UnsupportedOSPlatform("android")] [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] set; }
member this.OutputEncoding : System.Text.Encoding with get, set
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<set: System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.OutputEncoding : System.Text.Encoding with get, set
Public Shared Property OutputEncoding As Encoding
属性值
用于写入控制台输出的编码。
- 属性
例外
集运算中的属性值为 null
。
执行此操作期间出错。
你的应用程序没有执行此操作的权限。
注解
控制台使用输出编码将应用程序编写的字符转换为相应的控制台显示字符。 控制台使用的默认代码页由系统区域设置确定。
从 .NET Framework 4 开始,属性获取操作可能会返回缓存值,而不是控制台的当前输出编码。 如果属性的值OutputEncoding被属性赋值OutputEncoding以外的某种方式修改,例如调用WindowsSetConsoleOutputCP
函数,则可能会出现这种情况。
调用方说明
在 Unicode 编码中,Console类支持使用类进行 UTF-8 编码UTF8Encoding,从 .NET Framework 4.5 开始,它支持类的 UnicodeEncoding UTF-16 编码。 不支持使用类的 UTF32Encoding UTF-32 编码。 尝试将输出编码设置为 UTF-32 会引发一个 IOException。
请注意,成功向控制台显示 Unicode 字符需要以下各项:
主机必须使用 TrueType 字体(如 Lucida Console 或 Consolas)来显示字符。
主机使用的字体必须定义要显示的特定字形或字形。 如果基本字体不包含该字形的定义,控制台可以利用字体链接来显示链接字体中的字形。
有关控制台对 Unicode 编码的支持的详细信息,请参阅类中的 Console “控制台支持”部分。