title: Creating a UTF8 text file instead of ANSI
body: <p>I have this snippet of code to write a file asynchronously:</p>
  
  <pre><code>    private static async Task WriteTextAsync(string filePath, string text)
      { //Writes to our output files
          byte[] encodedText = Encoding.UTF8.GetBytes(text);
          using (FileStream sourceStream = new FileStream(filePath,
              FileMode.Create, FileAccess.Write, FileShare.None,
              bufferSize: 4096, useAsync: true))
          {
              await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
          };
      }
  </code></pre>
  
  <p>The created text file is still ANSI format despite having set the <code>Encoding.UTF8</code>.  There's 15 overloaded constructors for the <code>FileStream</code> class, and it's not obvious at all to me where I should set this if not on the encoded text.</p>
  
  <p>I can tell the file is ANSI, because when I open it in TextPad and view the file statistics it lists ANSI as the Code Set:</p>
  
  <p><img src="https://i.stack.imgur.com/kRIJb.png" alt="enter image description here"></p>
  
  <p>Having problems, because MySQL <code>LOAD INFILE</code> is not reading the file properly, after reading the answers I believe it has something to do with the BOM, but not sure.</p>
  
  <p>I tried this (for BOM):</p>
  
  <pre><code>        byte[] encodedText = new byte[] { 0xEF, 0xBB, 0xBF }.Concat(Encoding.UTF8.GetBytes(text)).ToArray();
          using (FileStream sourceStream = new FileStream(filePath,
              FileMode.Create, FileAccess.Write, FileShare.None,
              bufferSize: 4096, useAsync: true))
          {
              await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
          };
  </code></pre>
  
  <p>Textpad then saw it as UTF8, MySQL <code>LOAD INFILE</code> still failed.  Resaved in Textpad, and MySQL saw it properly.</p>
  
  <p>Changed code to this:</p>
  
  <pre><code>        using (TextWriter writer = File.CreateText(filePath))
          {
              await writer.WriteAsync(text);
          }
  </code></pre>
  
  <p>This seemed to work in both.  I'm not sure what the issue is with MySQL <code>LOAD INFILE</code> regarding this.</p>