Antivirus significantly slowing down writing to disk, is there a workaround that prevents storing data in memory?
<p>Let's say I'm receiving a file over a socket stream, I am receiving 1024 bytes at a time. Each time I write to the harddisk, my antivirus scans the entire file. The bigger the file gets, the longer it takes to write the next 1024 bytes. Not to mention the "file is in use by another process" errors.</p>

<p>My workaround at the moment is to just store the bytes in a byte array in the memory, up to X megabytes (user defined), the byte array is appended to the file on the harddisk every time it fills up.</p>

<pre><code>byte[] filebytearray = new byte[filesize]; //Store entire file in this byte array.

do
{
    serverStream = clientSocket.GetStream();
    bytesRead = serverStream.Read(inStream, 0, buffSize); //How many bytes did we just read from the stream?
    recstrbytes = new byte[bytesRead]; //Final byte array this loop
    Array.Copy(inStream, recstrbytes, bytesRead); //Copy from inStream to the final byte array this loop
    Array.Copy(recstrbytes, 0, filebytearray, received, bytesRead); //Copy the data from the final byte array this loop to filebytearray

    received += recstrbytes.Length; //Increment bytes received

}while (received &lt; filesize);

addToBinary(filebytearray, @"C:\test\test.exe"); //Append filebytearray to binary
</code></pre>

<p>(In this simplified example it just stores the entire file in memory before unloading it to hdd)</p>

<p>But I absolutely hate this method because it significantly increases the memory my program uses.</p>

<p>How do other programmers tackle this issue? When I download with firefox, as an example, it just downloads with full speed, my AV doesn't seem to pick it up until it's done, and it barely increases the process' memory usage. What's the big secret here?</p>

<p>Append to binary function I am using (WIP):</p>

<pre><code>private bool addToBinary(byte[] msg, string filepath)
{
    Console.WriteLine("Appending "+msg.Length+" bytes of data.");

    bool succ = false;

    do
    {
        try
        {
            using (Stream fileStream = new FileStream(filepath, FileMode.Append, FileAccess.Write, FileShare.None))
            {
                fileStream.Write(msg, 0, msg.Length);
                fileStream.Flush();
                fileStream.Close();
            }
            succ = true;
        }
        catch (IOException ex) { /*Console.WriteLine("Write Exception (addToBinary) : " + ex.Message);*/ }
        catch (Exception ex) { Console.WriteLine("Some Exception occured (addToBinary) : " + ex.Message); return false; }
    } while (!succ);
    return true;
}
</code></pre>
