Domanda

I need to read all bytes of a file and write this byte array to another file. I.e. I need the same behavior of File.ReadAllBytes and File.WriteAllBytes in Windows Mobile 6.1.

What is the fastest method to do this work?

È stato utile?

Soluzione

Do you actually need the whole file in memory at any time? If not, I'd just use something like:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32 * 1024]; // Or whatever size you want
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}

Then just open each file accordingly:

using (Stream input = File.OpenRead(...), output = File.OpenWrite(...))
{
    CopyStream(input, output);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top