0 out of 1 rated this helpful - Rate this topic

Console.OutputEncoding Property

Updated: March 2012

Gets or sets the encoding the console uses to write output.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static Encoding OutputEncoding { get; set; }

Property Value

Type: System.Text.Encoding
The encoding used to write console output.
Exception Condition
ArgumentNullException

The property value in a set operation is null.

PlatformNotSupportedException

This property's set operation is not supported on Windows 98, Windows 98 Second Edition, or Windows Millennium Edition.

IOException

An error occurred during the execution of this operation.

SecurityException

Your application does not have permission to perform this operation.

The console uses the output encoding to translate characters written by an application into corresponding console display characters. The output encoding incorporates a code page that maps 256 characters to individual console display characters. Different code pages include different special characters, typically customized for a language or a group of languages.

Starting with the .NET Framework version 4, a property get operation may return a cached value rather than the console's current output encoding. This can occur if the value of the OutputEncoding property is modified by some means other than an assignment to the OutputEncoding property such as calling the Windows SetConsoleOutputCP function.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • UIPermission  

    for reading and writing to top-level windows and subwindows. Associated enumeration: UIPermissionWindow.SafeTopLevelWindows

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

March 2012

Noted that the property value may be cached.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Cached result

This property return cached result, not current value.

More Information, Please

I can see no evidence that the value of the OutputEncoding property is cached. Can you supply some code that illustrates that the property returns a previous value rather than the current value?

Thanks.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

Proofs:

PowerShell:

chcp 1251
[console]::InputEncoding.CodePage #return 1251 (current value)
chcp 1250
[console]::InputEncoding.CodePage #return 1251 (cached value)
powershell [console]::InputEncoding.CodePage #return 1250 (current value)

OutputEncoding Is Cached

Thanks for providing the repro, and I apologize for my obtuseness. You're quite correct. In the .NET Framework Version 4, if the console's output encoding is modified by some means other than an assignment to the Console.OutputEncoding property, a property get operation may return a cached value. That's the case in your PowerShell example, where you've modified the input encoding using chcp. It's also the case if the input encoding is modified through the Windows SetConsoleCP function or the output encoding is modified through the SetConsoleOutputCP function. The Console class does not currently provide the functionality to clear the cached value and force a re-reading of the value of the current input or output encoding.

We'll modify the documentation during the next online documentation refresh to note that console encoding information is cached.

If this is an issue for you and you would like for the BCL team to reconsider this behavior, please submit a Connect bug at http://connect.microsoft.com/VisualStudio. This will also allow other developers to vote for whether they would like to see this behavior changed.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation