Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://msdn.microsoft.com/en-us/library/ms143376.aspx" rel="nofollow noreferrer">File.WriteAllText</a> uses a StreamWriter behind the scenes:</p> <pre><code>public static void WriteAllText(string path, string contents, Encoding encoding) { using (StreamWriter writer = new StreamWriter(path, false, encoding)) { writer.Write(contents); } } </code></pre> <p>You have a string already, so converting it to a byte array would be pointless, because this is performed inside the <a href="http://msdn.microsoft.com/en-us/library/system.io.streamwriter.flush.aspx" rel="nofollow noreferrer">StreamWriter.Flush</a> method anyway using its underlying <a href="http://msdn.microsoft.com/en-us/library/system.text.encoder(VS.71).aspx" rel="nofollow noreferrer">Encoder</a> class. Flush is called by the <a href="http://msdn.microsoft.com/en-us/library/system.io.streamwriter.dispose.aspx" rel="nofollow noreferrer">StreamWriter.Dispose</a> method which the using clause calls. This is a snippet from the source of Flush via reflector:</p> <pre><code>int count = this.encoder.GetBytes(this.charBuffer, 0, this.charPos, this.byteBuffer, 0, flushEncoder); </code></pre> <p>You can see it has a charBuffer. That's just a char[] array which it writes to when you perform <a href="http://msdn.microsoft.com/en-us/library/ce2kyyb4.aspx" rel="nofollow noreferrer">StreamWriter.Write(string)</a>.</p> <p>So in short, you already have the string, just let the shorter File method perform the cascading calls, as it makes your source slightly more readable. Converting it to a byte array is also not necessary, as I mentioned the StreamWriter does it for you.</p> <p>If you're getting conversion issues then use Encoding.Unicode as the final parameter in the 3 parameter overload, as <a href="http://msdn.microsoft.com/en-us/library/ms143375.aspx" rel="nofollow noreferrer">File.WriteAllText(string,string)</a> uses UTF8 without a BOM.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload