File.Copy() crashes with very large files

  • Thread starter Thread starter cyberco
  • Start date Start date
C

cyberco

When I try to copy a very large file (>8mb) using File.Copy() I get the
following error (may contain spelling errors, I typed it over):

=============================
OutOfMemoryException
IOException
at System.IO.__Errot.WinIOError()
at System.IO.File.InternalCopy()

=============================

How can I prevent those exceptions? What is sane to do? First check the
file size before copying? But what would be the maximum possible size?

I'm using:
WM5 PPC
..Net CF 2.0
 
The native FileCopy API has the same problem. The workaround is to use a
stream and copy the file in smaller packets of a few k.
 
Wow - amazing. How can a QA plan not include thousands of tiny files,
medium files, and very large files? Do any CF calls rely on File.Copy? If
so, then those are also (potentially) broken.

Anyway... Does anyone know the optimal "few K" for a stream copy? I always
default to 8K or 16K, but I wonder how much difference there would be with
1K, 2K, 4K,..., 1MB?

Does this problem only occur with the CF or is it also in the non-CF
(desktop) code?

One question for those who have experienced the failure... Does File.Copy
(when it fails as mentioned below) leave part of the destination file?

Thanks,

Hilton
 
Chris,

I tried to repro this with a 360MB file, it worked just fine with CF 1 and
2. Any ideas on how/when it fails?

try
{
File.Copy (@"\SD Card\test\file0.bin", @"\SD Card\test\file1.bin",
true);
}
catch (Exception e)
{
MessageBox.Show (e.ToString(), "File.Copy");
}


Thanks,

Hilton
 
2. Any ideas on how/when it fails?

Nope. Copying an 8Mb file sometimes works, sometimes doesn't. Can't say
when.
 
IIRC the partial destination gets erased when the failure occurs. This
really isn't a case of "broken" as much as it is a case of the API using too
large of a transfer buffer and not having enough memory on large files when
memory pressure is high. The failure doesn't cause anything bad, it just
says there's not enough memory and rejects the attempt to copy.

-Chris
 
The only time I saw it was when I was moving a CE image of around 18MB on
the device and it probably only had around 20MB free. I think that you have
to have a lot of memory pressure for this to occur.
 
Back
Top