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?

有帮助吗?

解决方案

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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top