Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a fixed size file in .NET

Tags:

c#

.net

io

Is there a way in .NET to create a file with specific size without having to write to it?

I need to be able to write at specific position leaving empty space as needed. What would be the best way to achieve this?

like image 970
Arturo Martinez Avatar asked Mar 21 '23 15:03

Arturo Martinez


1 Answers

You may not know it, but you may be asking this question "How do I create sparse files in .Net?" See this question

The short answer is that you cannot create sparse files using managed code, however, you can using the native API's -- the problem is that sparse files "remain sparse". So, if you want to backfill later, you are probably better off not doing this. You can use an open FileStream and call SetLength() If you just want to avoid the detail of writing out explicit the explicit filler to your file, the O/S will still fill the "empty" portion of your file with NULs

public override void SetLength(
    long value
)

ADDED

Note that the file is not necessarily physically extended on disk at this instance. But if you read from the "tail gap" you will get NULs and if you write to the gap, the O/S will backfill the intervening space with NULs. NTFS maintains "valid length" and "file length" seperately. Calling SetLength does not change the valid length, just the file length. The "tail gap" is the region of the file spanning from the valid length and the file length. Note also that setting the valid length is supported by Windows. Writing to the tail gap or end of file also updates the valid length.

So, you may avoid a performance hit by not having to actually extend the file immediately, but sooner or later the O/S will have to take the time to backfill with NUL filled blocks (and setting the valid length to the new write position), possibly leaving a smaller tail gap when you write to the tail gap.

Note that getting random blocks of existing data from the disk when you extend a file would be a security issue -- imagine getting payroll data with SSNs.

If Windows ever adds a new file system the exact behavior could become even more complicated, but is would presumably be at least as good as what happens on NTFS.

like image 176
Gary Walker Avatar answered Apr 04 '23 23:04

Gary Walker