This topic has not yet been rated - Rate this topic

Console.InputEncoding Property

Gets or sets the encoding the console uses to read input.

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

Property Value

Type: System.Text.Encoding
The encoding used to read console input.
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 input encoding to translate keyboard input into a corresponding character. The input encoding incorporates a code page that maps 256 keyboard character codes to individual 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 input encoding. This can occur if the value of the InputEncoding property is modified by some means other than an assignment to the InputEncoding property, such as calling the Windows SetConsoleCP function or using the chcp command from a PowerShell script.

.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.
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 InputEncoding 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)

InputEncoding 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 input encoding is modified by some means other than an assignment to the Console.InputEncoding 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

Reset input
Write access to this property reset input TestReader (http://msdn.microsoft.com/en-us/library/system.console.in.aspx) even if it was redirected (http://msdn.microsoft.com/en-us/library/system.console.setin.aspx).