How to "ToString" Character Arrays of Large Quantities?
<p>I have the following code:</p>

<pre><code>public static void PrependEntitiesToFile(string pathToFile)
{
    char[] buffer = new char[10000];
    FileInfo file = new FileInfo(pathToFile);

    string renamedFile = file.FullName + ".orig";
    System.IO.File.Move(file.FullName, renamedFile);

    using (StreamReader sr = new StreamReader(renamedFile))
    using (StreamWriter sw = new StreamWriter(file.FullName, false))
    {
        string entityDeclaration = "foo";
        sw.Write(entityDeclaration);
        string strFileContents = string.Empty;
        int read;
        while ((read = sr.Read(buffer, 0, buffer.Length)) &gt; 0)
        {
            for (int i = 0; i &lt; buffer.Length; i++)
            {
                strFileContents += buffer[i].ToString();
            }
        }
        sw.Write(strFileContents, 0, strFileContents.Length);

    }

    System.IO.File.Delete(renamedFile);
}
</code></pre>

<p>This code works but its SOOOOO SLOW because I am looping through each character in the <code>buffer</code> array and <code>ToString()</code>-ing it. Imagine how slow it is when its ingesting and duplicating a 823,000 character file. </p>

<p>Is there a way to convert a fully populated <code>buffer</code> to a string and add it at once rather than looping through each character in the array? Or, generically speaking, is there just a faster way to this?</p>

<p>I am so ashamed of this code.</p>
