문제

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