title: Reading unicode from console
body: <p>I am trying to read unicode string from a console in C#, for the sake of example, lets uset his one:</p>
  
  <p><strong>c:\SVN\D³ebugger\src\виталик\Program.cs</strong></p>
  
  <p>At first I just tried to <code>Console.ReadLine()</code> which returned me <code>c:\SVN\D3ebugger\src\???????\Program.cs</code></p>
  
  <p>I've tried to set the Console.InputEncoding to UTF8 like so <code>Console.InputEncoding = Encoding.UTF8</code> but that returned me <code>c:\SVN\D³ebugger\src\???????\Program.cs</code>, basically mucking up the Cyrillic part of the string.</p>
  
  <p>So randomly stumbling I've tried to set the encoding like that, <code>Console.InputEncoding = Encoding.GetEncoding(1251);</code> which returned <code>c:\SVN\D?ebugger\src\виталик\Program.cs</code>, this time corrupting the ³ character.</p>
  
  <p>At this point it seems that by switching <a href="http://msdn.microsoft.com/en-us/library/cc195051.aspx" rel="nofollow">encodings</a> for the InputStream I can only get a single language at a time.</p>
  
  <p>I've also tried going native and doing something like that:</p>
  
  <pre><code>// Code
  public static string ReadLine()
  {
      const uint nNumberOfCharsToRead = 1024;
      StringBuilder buffer = new StringBuilder();
  
      uint charsRead = 0;
      bool result = ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), buffer, nNumberOfCharsToRead, out charsRead, (IntPtr)0);
  
      // Return the input minus the newline character
      if (result &amp;&amp; charsRead &gt; 1) return buffer.ToString(0, (int)charsRead - 1);
      return string.Empty;
  }
  
  // Extern definitions
  
      [DllImport("Kernel32.DLL", ExactSpelling = true)]
      internal static extern IntPtr GetStdHandle(int nStdHandle);
  
      [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
      static extern bool ReadConsoleW(IntPtr hConsoleInput, [Out] StringBuilder lpBuffer, 
          uint nNumberOfCharsToRead, out uint lpNumberOfCharsRead, IntPtr lpReserved);
  </code></pre>
  
  <p>That was working fine for non-unicode strings, however, when I tried to make it read my sample string, the application crashed. I've tried to tell Visual Studio to break on ALL exception (including native ones), yet, the application would still crash.</p>
  
  <p>I also found <a href="http://connect.microsoft.com/VisualStudio/feedback/details/652017/bcl-reading-unicode-input-from-console-not-supported" rel="nofollow">this</a> open bug in Microsoft's Connect that seems to say that it is impossible right now to read Unicode from the console's InputStream.</p>
  
  <p>It is worth noting, even though not strictly related to my question, that Console.WriteLine is able to print this string just fine, if Console.OutputEncoding is set to UTF8.</p>
  
  <p>Thank you!</p>
  
  <h1>Update 1</h1>
  
  <p>I am looking for a solution for .NET 3.5</p>
  
  <h1>Update 2</h1>
  
  <p>Updated with the full native code I've used.</p>