How can I limit file fragmentation while working w

2020-02-12 10:36发布

问题:

In my application I am gathering small data bits into a bigger file over time. As a result, the target file becomes excessively fragmented. What can be done to limit fragmentation of the output file with .NET?

回答1:

You could deliberately increment the file size in larger chunks and internally monitor where the end of your current usage is. That way you give the system a better chance of allocating contiguous space for your file.



回答2:

The only way you can reliably reduce file fragmentation is to allocate space for the complete file in one go (fill the rest of the file with zeros, as filler). But this can only be done if you have some idea of the final size of the file.

Another option would be to increase the file size by a certain value during each increment (say, 100 MB). That way you would have lesser defragmented chunks.