FileStream.SetLength() Not Creating Empty Space

  • Thread starter Thread starter Charles Teel
  • Start date Start date
C

Charles Teel

I've noticed that whenever I use FileStream.SetLength() on a file that
is on the emulator, the file is empty (filed with nothing but zeros).
But whenever I use it on an actual device it is not empty. In some
cases its filled with data that was stored in files that had since
been deleted.

Is there a way to SetLength() a file and have the new space be filled
with zeros other than going in an actuall writing a bunch of zeros?
The key thing I need is speed.
 
The behavior you see is likely the way the devices file system driver works.
If it initialized to all zeros that would actually be slower because it
would have to go in and write zeros for you, so what you see now is actually
faster. If you want it to be cleared, you'll have to manually go in and
write zeros. I suspect the most efficient mechanism to do that is to create
an empty byte array and copy that to the stream - probably in 4k chunks
(since that's a page size).
 
Back
Top