title: Reading UTF8-encoded command-line in .NET (C#)
body: <p>I want to be able to handle UTF8-encoded command-line parameters in a .NET console program I'm writing.  Unfortunately, both the "args" array passed to the Main() function, and the Environment class members (CommandLine and GetCommandLineArgs()) have aleady been (incorrectly) converted into Unicode, seemingly by treating the command-line as single-byte extended-ASCII.</p>
  
  <p>For example, U+2019 (closing single apostrophe) in UTF8 is 0xe2 0x80 0x99.  In the 1252 code-pade, 0x80 is the Euro symbol (U+20ac) and 0x99 is the "TM" symbol (U+2122). (The 0xe2 is an "a" with circumflex, which is the U+00e2, so doesn't change)  When I pass these three bytes in on the command-line, the "char" elements of the string are 0x00e2 0x20ac and 0x2122.</p>
  
  <p>Is there a way of either telling .NET to interpret the command-line as UTF8, or get the raw, unprocessed command-line (which I can happily convert to a Unicode string)?</p>
  
  <h2>Update</h2>
  
  <p>(<em>Following dletozeun's answer</em>)</p>
  
  <p>Windows does odd things -- especially if it's XP (which I was using when I first asked the question).  It seems things behave differently whether you're trying to call the .NET command-line program from a batch file or direct from a command-prompt.  There's possibly a <em>very good reason</em>™ for this, but I don't know it.  Anyway, should it help anyone, here's what I've found:</p>
  
  <h3>Command Line</h3>
  
  <p>Opening a standard Command Prompt window and entering the following command:</p>
  
  <pre><code>UTF8Cmd.exe abcâ€™def
  </code></pre>
  
  <p>where <code>UTF8Cmd</code> is a test program incorporating dletozeun's solution, and the middle characters are 0xe2, 0x80, 0x99 (the UTF8 bytes for U+2019 -- closing single apostrophe) produces the following output (showing the argument before and after dletozeun's code, both as a string and dumped in hex):</p>
  
  <pre><code>    Raw : "abcâ?Tdef"    61 62 63 e2 20ac 2122 64 65 66
      UTF8: "abc'def"      61 62 63 2019 64 65 66
  </code></pre>
  
  <p>showing that the original arguments (<code>Raw</code>) have been mangled from the 1252-code-page byte values into their Unicode equivalents, but that the code posted has converted them back to the correct value (U+2019).</p>
  
  <h3>Batch File</h3>
  
  <p>Unfortunately, putting just the above into a batch file does not work... a completely different mangling happens, producing:</p>
  
  <pre><code>    Raw : "abcÔÇÖdef"    61 62 63 d4 c7 d6 64 65 66
      UTF8: "abc???def"    61 62 63 fffd fffd fffd 64 65 66
  </code></pre>
  
  <p>the raw bytes have been mangled into something weird, which are presumably not valid UTF8, hence the <code>fffd</code>s after processing.</p>
  
  <p><strong>However</strong>, @mvp's suggestion of using <code>chcp 65001</code> first (and resetting it afterwards) now <em>does</em> make things work <em>without</em> needing dletozeun's code:</p>
  
  <pre><code>Active code page: 65001
      Raw : "abc’def"      61 62 63 2019 64 65 66
      UTF8: "abc�def"      61 62 63 fffd 64 65 66
  Active code page: 850
  </code></pre>
  
  <p>I <strong>had</strong> tried this before, as noted in my comment below, but that was on an XP box which totally fails (it doesn't even appear to run the command, and leaves the command-prompt in a weird state).  Trying just now in response to the answer -- on a Windows 7 box -- and the <code>chcp 65001</code> command works as I hoped it would when I originally asked the question!</p>