tags:

views:

1003

answers:

3

I get the following error:

Unhandled Exception: System.IO.IOException: The parameter is incorrect.
 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 at System.IO.__Error.WinIOError()
 at System.Console.set_OutputEncoding(Encoding value)
 at (my program)

when I run the following line of code:

 Console.OutputEncoding = Encoding.Unicode;

Any idea why? I do not get this error if I set the encoding to UTF8 instead.

A: 

I think it has to do with the CodePage of the Encoding you are using. In particular see SetConsoleOutputCP Function. I don't know much more, sorry.

Edit: I reported the reference to the SetConsoleOutputCP because this function is internally called (through PInvoke) by the (set operation of) Console.OutputEncoding.

Fabrizio C.
+1  A: 

Encoding.Unicode is UTF-16 which uses 2 bytes to encode all characters. The ASCII characters (English characters) are the same in UTF-8 (single bytes, same values), so that might be why it works.

My guess is that the Windows Command Shell doesn't fully support Unicode. Funny that the Powershell 2 GUI does support UTF-16 (as far as I know), but the program throws the same exception there.

The following code works which shows that the Console object can have its output redirected and support Encoding.Unicode:

FileStream testStream = File.Create("test.txt");
TextWriter writer = new StreamWriter(testStream, Encoding.Unicode);
Console.SetOut(writer);            
Console.WriteLine("Hello World: \u263B");  // unicode smiley face
writer.Close(); // flush the output
A: 

According to the list of Code Page Identifiers on MSDN, the UTF-16 and UTF-32 encodings are managed-only:

1200   utf-16       Unicode UTF-16, little endian byte order (BMP of ISO 10646); available only to managed applications
1201   unicodeFFFE  Unicode UTF-16, big endian byte order; available only to managed applications
12000  utf-32       Unicode UTF-32, little endian byte order; available only to managed applications
12001  utf-32BE     Unicode UTF-32, big endian byte order; available only to managed applications

For instance, they're not listed in the registry with the other system code pages under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage.

Trevor Robinson